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

SQL for total sales for each customer for each year

使用 Advantage 数据库服务器 11 我试图逐年比较每个客户从两种类型中购买的总金额,但出现错误:

[iAnywhere 解决方案][优势 SQL 引擎]预期 未找到词法元素:THEN

这是table。

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

您的 CASE 声明中缺少 THEN

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