我需要列出每月显示利润的所有城区。
我不知道如何将产品的计数值与子查询中的月份联系起来。
有人可以帮我一把吗?
SELECT
districts.id,
districts.name,
mmonth.m10
FROM districts
INNER JOIN
(SELECT
SUM(product_order.price_of_day * product_order.amount) AS m10
FROM product_order
INNER JOIN orders ON product_order.order_id = orders.id
WHERE MONTH(orders.date_time) = 10 ) mmonth
INNER JOIN cities ON cities.id = districts.city_id
WHERE cities.id = 2;
http://sqlfiddle.com/#!2/8fb34/4
看看我需要什么。
| id | name | m10 | m11 | m12 |
------------------------------------
| 3 | district 3 | 730 | 360 | 453 |
------------------------------------
| 4 | district 4 | 730 | 250 | 856 |
-------------------------------------
我如何总结这样的月份?
| id | name | m10 | m11 | m12 | total |
---------------------------------------------
| 3 | district 3 | 730 | 360 | 453 | 1543 |
---------------------------------------------
| 4 | district 4 | 730 | 250 | 856 | 1836 |
---------------------------------------------
答案 0 :(得分:1)
修改强>
这应该这样做:
SELECT
districts.id,
districts.name,
SUM(IF(MONTH(orders.date_time) = 10, product_order.price_of_day * product_order.amount, 0)) AS m10,
SUM(IF(MONTH(orders.date_time) = 11, product_order.price_of_day * product_order.amount, 0)) AS m11,
SUM(IF(MONTH(orders.date_time) = 12, product_order.price_of_day * product_order.amount, 0)) AS m12,
SUM(product_order.price_of_day * product_order.amount) AS total
FROM cities
LEFT JOIN districts ON cities.id = districts.city_id
LEFT JOIN orders ON orders.district_id = districts.id
LEFT JOIN product_order ON product_order.order_id = orders.id
WHERE cities.id = 2
GROUP BY districts.id;
答案 1 :(得分:1)
您应该使用GROUP BY
(请参阅:http://technet.microsoft.com/en-us/library/ms177673.aspx)
SELECT
[IdentifyingDataColumnName]
...
,[IdentifyingDataColumnName1]
,[CalculatedField] = SUM(<column name(/s) / Calculation to sum>)
FROM
Table 1
LEFT JOIN 2
GROUP BY
[IdentifyingDataColumnName]
...
,[IdentifyingDataColumnName1]
HAVING
SUM(<column name(/s) / Calculation to sum>) > 0
重新阅读你的问题(并意识到我完全误读了它!)你可以做到以下几点:
SELECT
districts.id
,districts.name
,SUM(CASE WHEN MONTH(orders.date_time) = 10 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m10
,SUM(CASE WHEN MONTH(orders.date_time) = 11 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m11
,SUM(CASE WHEN MONTH(orders.date_time) = 12 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m12
FROM
districts
INNER JOIN cities ON cities.id = districts.city_id
INNER JOIN orders ON districts.id = orders.district_id
INNER JOIN product_order ON product_order.order_id = orders.id
WHERE
cities.id = 2
GROUP BY
districts.id
,districts.name
添加总列数并不容易,只需删除CASE... WHEN... THEN... ELSE... END
即可将它们全部加起来;下面的代码也被修改为使用LEFT JOIN
,以确保如果没有订单的城市存在区域,它仍然会列出月值为0(有关详细信息,请参阅:http://technet.microsoft.com/en-us/library/aa178429(v=SQL.80).aspx )
SELECT
districts.id
,districts.name
,SUM(CASE WHEN MONTH(orders.date_time) = 10 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m10
,SUM(CASE WHEN MONTH(orders.date_time) = 11 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m11
,SUM(CASE WHEN MONTH(orders.date_time) = 12 THEN (product_order.price_of_day * product_order.amount) ELSE 0 END) AS m12
,SUM(product_order.price_of_day * product_order.amount) AS Total
FROM
districts
INNER JOIN cities ON cities.id = districts.city_id
LEFT JOIN orders ON districts.id = orders.district_id
LEFT JOIN product_order ON product_order.order_id = orders.id
WHERE
cities.id = 2
GROUP BY
districts.id
,districts.name