每位客户每年总销售额的SQL

时间:2019-09-24 23:52:07

标签: sql sum advantage-database-server

使用Advantage Database服务器11 我试图逐年查找两种类型的每位客户的总购买额,但出现错误:

[iAnywhere解决方案] [Advantage SQL Engine]预期 找不到词法元素:THEN

这是桌子。

customernr | Date_in | Status | InvType | Qty | Total
1111          9/1/2018            D         5    25.00
1111         12/1/2018   V        D         3    15.00
1111         5/12/2019            L         1     2.00
1111         7/11/2019            D         5    35.00
1112         6/21/2018            L         7    18.00
1112         9/14/2019            L         3     7.00


Select 
customernr,
SUM(CASE WHEN date_in >= '2018-01-01' and date_in <= '2018-12-31' total 
and InvType='D' ELSE 0 END) LastYD,
SUM(CASE WHEN date_in >= '2018-01-01' and date_in <= '2018-12-31' total 
and InvType='L' ELSE 0 END) LastYL,
SUM(CASE WHEN date_in >= '2019-01-01' and date_in <= '2019-12-31' total 
and InvType='D' ELSE 0 END) ThisYD,
SUM(CASE WHEN date_in >= '2019-01-01' and date_in <= '2019-12-31' total 
and InvType='L' ELSE 0 END) ThisYL

from invoice
where (Status <> 'V' or Status IS NULL)

Group by Customernr

感谢您的帮助,

KHJ

1 个答案:

答案 0 :(得分:2)

您的THEN语句中缺少CASE

Select 
customernr,
SUM(CASE WHEN date_in >= '2018-01-01' and date_in <= '2018-12-31' 
and InvType='D' THEN total ELSE 0 END) LastYD,
SUM(CASE WHEN date_in >= '2018-01-01' and date_in <= '2018-12-31' 
and InvType='L' THEN total ELSE 0 END) LastYL,
SUM(CASE WHEN date_in >= '2019-01-01' and date_in <= '2019-12-31' 
and InvType='D' THEN total ELSE 0 END) ThisYD,
SUM(CASE WHEN date_in >= '2019-01-01' and date_in <= '2019-12-31' 
and InvType='L' THEN total ELSE 0 END) ThisYL

from invoice
where (Status <> 'V' or Status IS NULL)

Group by Customernr