how to compute frequency using snowflake and rolling windows

Viewed 27

I am trying to compute the order frequency on different market on each day of the last 365 days. with my query I am able to get this results :

country frequency
FR 6.6
DE 4.5
BE 5.8

my goal is to get this :

country frequency date
FR 6.6 2022/09/19
DE 4.5 2022/09/19
BE 5.8 2022/09/19
FR 6.4 2022/09/18
DE 4.7 2022/09/18
BE 5.4 2022/09/18

my query looks like this :

'''

select x.COUNTRYCODE,  sum(z.order_count)/count(x.UCD_GR_ID) as frequency

from 

(SELECT o.COUNTRYCODE , o.UCD_GR_ID 

FROM ddddddddddddd o
    
INNER JOIN sssssssss m
    ON o.CHANNEL_UPPER = m.CHANNEL_UPPER
    AND o.COUNTRYCODE = m.COUNTRY_CODE_UPPER
    AND o.SOURCE_POS_SYSTEM_UPPER = m.SOURCE_SYSTEM_UPPER
    
WHERE  DATEDIFF('DAY', o.ORDER_DATE,GETDATE()) <= 100
    AND o.ORDER_STATUS_POS_CODE = 4
    AND m.CHANNEL_TYPE_UPPER IN  ('APP')
    --and o.COUNTRYCODE = 'BE'
    and UCD_GR_ID is not NULL
group by o.COUNTRYCODE, o.UCD_GR_ID)
x
 
 JOIN

(select o.COUNTRYCODE , o.UCD_GR_ID  ,count(o.ORDER_DATE) as order_count
from ddddddddddddd  o
 where DATEDIFF('DAY', o.ORDER_DATE,GETDATE()) <= 364
 AND o.ORDER_STATUS_POS_CODE = 4
 --and o.COUNTRYCODE = 'BE'
 and UCD_GR_ID is not NULL
group by o.COUNTRYCODE, o.UCD_GR_ID )z
ON x.COUNTRYCODE = z.COUNTRYCODE
and x.UCD_GR_ID = z.UCD_GR_ID

group by x.COUNTRYCODE

'''

0 Answers
Related