0

我需要列出每个月显示利润的城市的所有地区。

不知道如何将产品的计数值与子查询中的月份联系起来。

有人可以帮帮我吗?

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  |
---------------------------------------------
4

2 回答 2

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;
于 2013-11-07T16:18:50.723 回答
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它,它就会将它们全部相加;下面的代码也被修改为使用 aLEFT 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
于 2013-11-07T16:33:03.207 回答