我试图建立一个SQL查询,以计算基于某些值的销售总额,如下所示。
下面是我的数据集的情况。
cust_name,sales_count,day_count
cust_a,100,3
cust_a,200,5
cust_a,150,7
cust_a,120,1
cust_a,180,10
cust_a,100,8
cust_b,20,3
cust_b,10,4
cust_b,50,6
cust_b,60,8
cust_b,15,9
我希望得到以下格式的输出结果
cust_name,sales_count,day_count
cust_a,280,last_14
cust_a,450,last_7
cust_b,85,last_14
cust_b,80,last_7
下面是我试图建立的案例语句
select cust_name,
sum(case when day_count > 7 then count(sales_count) else 0 end) as count_14,
sum(case when day_count < 7 then count(sales_count) else 0 end) as count_7
from sales
group by cust_name;
我使用的是Amazon Redshift数据库。
在这个链接中发现了一个类似的问题(Amazon Redshift - Get week wise sales count by category),但我一直得到聚合函数调用可能没有嵌套聚合或窗口函数。
谁能帮助解决这个问题。谢谢。