条件求和 (Alasql)

Conditional summing (Alasql)

我最近发现了 Alasql,它已经为我节省了大量的数据操作工作。

现在我正在尝试汇总每个客户的年收入。我试过分区但没有成功,我试过这种条件方法,但它只有 returns 2013 年或 2014 年的数据,而不是同时:

    var custTable = alasql('SELECT CUSTOMER as customer, 
SUM(REVENUE::NUMBER) AS revenueTotal, SUM(CASE WHEN YEAR = "2013" THEN 
REVENUE::NUMBER END) AS revenue2013,  SUM(CASE WHEN YEAR = "2014" THEN 
REVENUE::NUMBER END) AS revenue2014   FROM ? GROUP BY CUSTOMER', 
[revenueTestData]);

基本上我想从中得到:

{"YEAR":"2013","MONTH":"1","CUSTOMER":"Some Customer","REVENUE":"7533.36"}, {"YEAR":"2014","MONTH":"1","CUSTOMER":"Some Customer","REVENUE":"10000"}

为此:

{"CUSTOMER":"Some Customer","revenueTotal":"17533.36","revenue2013":7533.36","revenue2014":"10000"}

为什么只查询 return 2013 OR 2014,而不是两者?条件求和的正确方法是什么?

(顺便说一句,我无法为 ALASQL 创建新标签,但我认为在不久的将来它会对某些人派上用场 - 有人可以创建它吗?)

试试这个:

SUM(CASE WHEN YEAR = "2013" THEN REVENUE::NUMBER ELSE 0 END) AS revenue2013,  
SUM(CASE WHEN YEAR = "2014" THEN REVENUE::NUMBER ELSE 0 END) AS revenue2014