我想为每种产品获得2016年和2017年的销售总额。
我试过了:
Select
productid,
Switch(year(DATE)=2016,sum(salesa1)) as sales16,
Switch(year(DATE)=2017,sum(salesa1)) as sales17
From Sales
Group by productid
可能我有什么不对劲,有人可以帮助我吗?
答案 0 :(得分:2)
条件需要内部 sum()
:
Select productid,
sum(switch(year(DATE) = 2016, salesa1)) as sales16,
sum(switch(year(DATE) = 2017, salesa1)) as sales17
From Sales
Group by productid