D.Aquar
D.Aquar

Reputation: 33

Previous week data comparison SQL - Teradata

I have a table with sample data as below:

enter image description here

I am trying to calculate a current weekly data comparison with previous week value, and to make the output as below:

enter image description here

How can i achieve this:

Upvotes: 0

Views: 372

Answers (1)

Femmer
Femmer

Reputation: 132

This should work, just change the "#test" to your table name:

SELECT  t2.C_Date,
        SUM(t2.Prod1 + t2.Prod2 + t2.Prod3) AS Total_sale,
        SUM(t1.Prod1 + t1.Prod2 + t1.Prod3) AS Prev_Week_Total_sale
    FROM #test t1
    INNER JOIN #test t2
        ON t1.C_Date = DATEADD(DAY, -7, t2.C_Date)
            GROUP BY t2.C_Date

Upvotes: 1

Related Questions