I am trying to build a SQL query that would count the sum of sales made based on certain values as shown below:
Given below is how my dataset is:
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
I would like to get the output in the below format
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
Given below is the case statement I tried to build
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;
I am using a Amazon Redshift Database.
Found a similar issue in this link (Amazon Redshift - Get week wise sales count by category) but I keep getting aggregate function calls may not have nested aggregate or window function.
Could anyone help trouble shoot this. Thanks.
pendo_visitor_idcolumn from your sample data? - D-Shih