答案 0 :(得分:5)
您可以使用SUM(CASE...)
和GROUP BY
子句的组合。像这样的东西
SELECT
city,
SUM(CASE WHEN MONTH(orderDate)=1 THEN amount ELSE 0 END) as january,
SUM(CASE WHEN MONTH(orderDate)=2 THEN amount ELSE 0 END) as february,
SUM(CASE WHEN MONTH(orderDate)=3 THEN amount ELSE 0 END) as march
FROM orders
GROUP BY city
我假设表名为ORDERS
,并使用列orderDate
来识别mounth。
答案 1 :(得分:0)
如果我理解你写的,可能是这样的:
SELECT city, sum(amount)
FROM table_name
WHERE MONTH(date_column)=12
GROUP BY city;