2

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

现在我正在尝试汇总每个客户的年收入。我试过 partition by 没有成功,我试过这种条件方法,但它只返回 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","re​​venueTotal":"17533.36","re​​venue2013":7533.36","re​​venue2014":"10000"}

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

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

4

1 回答 1

4

尝试这个:

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 
于 2015-03-21T09:21:33.983 回答