0

我有四个表,Company、Vehicles、Quotes 和 PhoneRequests。

公司可能会收到有关其车辆的报价和请求。

每个报价和请求都标有时间。

只要报价或请求的数量大于 0,我想查找每个公司在每个月收到的报价和请求数量。

我不知道如何分组以使两个单独表中的月份相同。

这是我到目前为止所得到的:

SELECT
    company.name `company`,
    vehicle.name `vehicle`,
    DATE_FORMAT(COALESCE(quote.qtime, pr.date),'%Y-%m') `month`,
    COUNT(DISTINCT quote.qid) `quotes`,
    COUNT(DISTINCT pr.id) `phone_reqs`
FROM companies company
    LEFT JOIN vehicles vehicle ON vehicle.compid=company.cid
    LEFT JOIN quote_vehicle qv ON qv.vehicle_id=vehicle.vid
    LEFT JOIN quotes quote ON qv.quote_id=quote.qid
    LEFT JOIN phone_requests pr ON pr.vehicle_id=vehicle.vid AND quote.qtime IS NULL OR DATE_FORMAT(quote.qtime,'%Y-%m')=DATE_FORMAT(pr.date,'%Y-%m')
GROUP BY
    company.cid, `month`
HAVING
    quotes > 0 OR phone_reqs > 0
ORDER BY
    `month` DESC, `company` ASC

但它似乎将其归咎于phone_reqs错误的公司,可能是因为那AND quote.qtime IS NULL OR DATE_FORMAT(quote.qtime,'%Y-%m')=DATE_FORMAT(pr.date,'%Y-%m')一点。不过,我不知道如何确保报价和请求发生在同一个月内。

4

3 回答 3

1

你说

...可能是因为那AND quote.qtime IS NULL OR DATE_FORMAT(quote.qtime,'%Y-%m')=DATE_FORMAT(pr.date,'%Y-%m')一点。

添加一个简单的WHERE子句怎么样?

SELECT
    company.name `company`,
    vehicle.name `vehicle`,
    DATE_FORMAT(COALESCE(quote.qtime, pr.date),'%Y-%m') `month`,
    COUNT(DISTINCT quote.qid) `quotes`,
    COUNT(DISTINCT pr.id) `phone_reqs`
FROM companies company
    LEFT JOIN vehicles vehicle ON vehicle.compid=company.cid
    LEFT JOIN quote_vehicle qv ON qv.vehicle_id=vehicle.vid
    LEFT JOIN quotes quote ON qv.quote_id=quote.qid
    LEFT JOIN phone_requests pr ON pr.vehicle_id=vehicle.vid
WHERE
  (quote.qtime IS NULL) OR (quote.qtime BETWEEN pr.date AND pr.date - 2592000)
GROUP BY
    company.cid, `month`
HAVING
    quotes > 0 OR phone_reqs > 0
ORDER BY
    `month` DESC, `company` ASC

注意:2592000 代表 30 天(假设您的日期是时间戳)...

于 2013-05-04T23:33:24.997 回答
1

可能这可以帮助

SELECT
    company.name `company`,
    vehicle.name `vehicle`,
    month,
    quote.quotes,
    pr.phone_reqs
FROM companies company
    LEFT JOIN vehicles vehicle ON vehicle.compid=company.cid
    LEFT JOIN quote_vehicle qv ON qv.vehicle_id=vehicle.vid
LEFT JOIN (SELECT COUNT(DISTINCT qid) quotes, DATE_FORMAT(qtime,'%Y-%m') `month` FROM quotes GROUP BY `month`) quote ON quote.qid = qv.quote_id
LEFT JOIN (SELECT COUNT(DISTINCT id) `phone_reqs`, DATE_FORMAT(date,'%Y-%m') `month` FROM phone_requests GROUP BY `month`) pr ON pr.phone_reqs = vehicle.vid AND quote.month = pr.month
GROUP BY  company.cid
HAVING
    quotes > 0 OR phone_reqs > 0
ORDER BY
    `month` DESC, `company` ASC
于 2013-05-04T22:24:28.180 回答
0

想通了一些东西:

SELECT `month`, `company`, SUM(nquotes) nquotes, SUM(nphone) nphone  FROM ((
    SELECT
        DATE_FORMAT(quote.qtime,'%Y-%m') `month`,
        company.name `company`,
        COUNT(*) `nquotes`,
        0 `nphone`,
        company.cid `company_id`
    FROM
        companies company
        JOIN vehicles vehicle ON vehicle.compid=company.cid
        JOIN quote_vehicle qv ON qv.vehicle_id=vehicle.vid
        JOIN quotes quote ON qv.quote_id=quote.qid
    GROUP BY `month`, `company_id`
) UNION ALL (
    SELECT
        DATE_FORMAT(pr.date,'%Y-%m') `month`,
        company.name `company`,
        0 `nquotes`,
        COUNT(*) `nphone`,
        company.cid `company_id`
    FROM
        companies company
        JOIN vehicles vehicle ON vehicle.compid=company.cid
        JOIN phone_requests pr ON pr.vehicle_id=vehicle.vid
    GROUP BY `month`, `company_id`
)) master
GROUP BY `month`, `company_id`
ORDER BY `month` DESC, `company` ASC    
于 2013-05-04T22:58:22.247 回答