group by - Aggregating in SQL - Multiple Criteria -
this follow-up question aggregating in sql
i have table below.
conversion_date user_name last_date_touch touch_count 7/15/2017 6/17/2017 1 7/16/2017 b 6/24/2017 2 7/19/2017 6/20/2017 1 7/19/2017 c 6/29/2017 1 grouped conversion_date , user_name, sum of touch_count looking 30 days conversion date but @ same time keeping last_date_touch within 30 day window of conversion_date.
for example, if 30 days conversion_date = 7/19/2017, cannot take sum of touch_count 30 day window below because include first row has last_date_touch out of 30 day window of conversion date.
this attempt, don't think takes account last_date_touch being in 30 day window.
select a.user_name ,a.conversion_date ,sum(b.touch_count) sumover30days sometable left join sometable b on b.user_name = a.user_name , cast(b.conversion_date date) <= cast(a.conversion_date date) , cast(b.conversion_date date) >= cast(dateadd(day,-30,a.conversion_date) date) group a.user_name, a.conversion_date thanks!
Comments
Post a Comment