Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. Date UserCountToday UserCount7days
  2. 20120911 907575
  3. 20120910 953629
  4. 20120909 1366180
  5. 20120908 1388916
  6. 20120907 1009425
  7. 20120906 918638
  8. 20120905 956770
  9. 20120904 1018152
  10. 20120903 1306341
  11.  
  12. with cte as
  13. (select *,ROW_NUMBER() over(order by [Date]) as row_num
  14. from t_users)
  15. select [Date],UserCountToday, (select SUM(UserCountToday)
  16. from cte c1 where c.row_num>=c1.row_num
  17. and abs(c.row_num-c1.row_num)<7) as UserCount7days
  18. from cte c
  19.  
  20. Date UserCountToday UserCount7days
  21. 20120903 1306341 1306341
  22. 20120904 1018152 2324493
  23. 20120905 956770 3281263
  24. 20120906 918638 4199901
  25. 20120907 1009425 5209326
  26. 20120908 1388916 6598242
  27. 20120909 1366180 7964422
  28. 20120910 953629 7611710
  29. 20120911 907575 7501133
  30.  
  31. ;WITH CTE as (select row_number() over (order by date) as rn,Date,UserCountToday,UserCount7days from tbl5)
  32.  
  33. ,CTERec as (
  34. select rn,Date,UserCountToday ,UserCountToday as UserCount7days from CTE where rn=1
  35. union all
  36. select c.rn,c.Date,c.UserCountToday ,c.UserCountToday+c1.UserCount7days as UserCount7days from CTERec c1 inner join CTE c
  37. on c.rn=c1.rn+1
  38. )
  39.  
  40. select Date,UserCountToday,CASE WHEN rn<=7 then UserCount7days else (UserCount7days-(select UserCount7days from CTERec where rn=c.rn-7)) end asUserCount7days from CTERec c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement