How to convert this query to 52 weeks rolling based data.

RXR 121 Reputation points
2020-09-28T02:51:48.833+00:00

I have CTE which is quite manual so everytime I have to change week number and days thing in this query.

With CTE_Deprivation_Live20 As (  
 Select m.PeopleID ,DateDiff(yy,P.DateOfBirth,GetDate()) as Age, m.LocationName,m.ActiveStart,m.ActiveEnd,  
  
 DatePart(wk,DateAdd(Week,-17,GETDATE())) WeekNo, DatePart(Year,GetDate() -1) year,  
 sum(case when M.ActiveStart < DateAdd(day,-4,DateAdd(Week,-17,GETDATE())) AND M.ActiveEnd >DateAdd(day,+2,DateAdd(Week,-17,GETDATE())) or M.ActiveEnd is null then 1 else 0 end) as LiveMem  
   ,sum(case when M.ActiveEnd BETWEEN DateAdd(day,-4,DateAdd(Week,-17,GETDATE())) and DateAdd(day,+2,DateAdd(Week,-17,GETDATE()))  then 1 else 0 end) as No_of_Member_Cancelled  
     
From AX.Memberships m  
Join AX.People P  
On  M.PeopleID = P.PeopleID  
   
Group by m.PeopleIDm.LocationName,m.ActiveStart,m.ActiveEnd,p.SA1,p.DateOfBirth  
)  
Select * from CTE_Deprivation_Live20  

Right now I have in my database :

28547-demo.png

Required Output:

28583-output.png

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,981 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,654 questions
{count} votes

2 answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,581 Reputation points
    2020-09-28T03:25:01.177+00:00

    Hi @RXR

    You need to create a sql server job to perform operations regularly every week.
    Does your query calculate the week number based on the current date of the system? If it is, then the problem is relatively simple. Every time job is automatically executed, the current week number will be calculated.

    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Erland Sommarskog 112.7K Reputation points MVP
    2020-09-28T22:15:40.193+00:00

    Like Echo I have problems understanding what you ask for. You talk about 52 weeks in the title, but the query you post seem to work with 17 weeks.

    Is that you want want to change 17 to 52, 12 or whatever every time? In that case you can encapsulate the query into a function:

    CREATE FUNCTION YourFunc(@weeks int) RETURNS TABLE AS
    RETURN (
         With CTE_Deprivation_Live20 As (
          Select m.PeopleID ,DateDiff(yy,P.DateOfBirth,GetDate()) as Age, m.LocationName,m.ActiveStart,m.ActiveEnd,
    
          DatePart(wk,DateAdd(Week,@weeks, GETDATE())) WeekNo, DatePart(Year,GetDate() -1) year,
          sum(case when M.ActiveStart < DateAdd(day,-4,DateAdd(Week, @weeks ,GETDATE())) AND M.ActiveEnd >DateAdd(day,+2,DateAdd(Week,@weeks,GETDATE())) or M.ActiveEnd is null then 1 else 0 end) as LiveMem
            ,sum(case when M.ActiveEnd BETWEEN DateAdd(day,-4,DateAdd(Week,@weeks,GETDATE())) and DateAdd(day,+2,DateAdd(Week,@weeks,GETDATE()))  then 1 else 0 end) as No_of_Member_Cancelled
    
         From AX.Memberships m
         Join AX.People P
         On  M.PeopleID = P.PeopleID
    
         Group by m.PeopleIDm.LocationName,m.ActiveStart,m.ActiveEnd,p.SA1,p.DateOfBirth
         )
         Select * from CTE_Deprivation_Live20
    )
    go
    SELECT * FROM YourFunc(52)
    

    But this is a bit of a guess of what you are really asking for.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.