我觉得这是一个非常基本的问题,但这让我很沮丧!想象一下,我有一个表格(收入),大致是这样的:
Month Product Revenue
2019-01 Networking 1.00
2019-01 Computing 2.53
2019-01 Storage 4.88
2019-02 Networking 5.32
2019-02 Storage 6.20
...
我想做的就是总结每月总收入和每月网络收入,但是对于我来说,我无法全神贯注于d这样做(不编写两个查询)。因此结果应类似于:
Month TotalRevenue NetworkingRevenue
2019-01 8.41 1.00
2019-02 11.52 5.32
...
任何帮助将不胜感激。
答案 0 :(得分:1)
使用情况
select month,
sum(case when Product='Networking' then Revenue else 0 end) as NetworkingRevenue,
sum(Revenue) as TotalRevenue from table_name group by month
答案 1 :(得分:0)
尝试一下:
SELECT
Month,
SUM(revenue) AS Revenue,
SUM(CASE WHEN Product = 'Networking' THEN Revenue ELSE 0 END) AS Revenue_Networking
FROM MyTable
GROUP BY Month
ORDER BY Month