0

In my code below the row data are not grouping into month by month name. I want to group row data of the same month.

Date Revenue
2023-03-01 500
2023-03-29 300
2023-04-15 600
2023-05-11 300
2023-05-23 600
2023-05-31 500
SELECT  
    DATENAME(month, OrderDate) as Month,
        Sum(Amount) as revenue,
        Amount - LAG (Amount) OVER (ORDER BY OrderDate ASC) AS Revenue_growth       
FROM OrderDetail
Group by OrderDate

Expected output

Date Revenue Revenue_growth
March 800 0
April 600 -200
May 1400 800
User_K47
  • 49
  • 4
  • Why are you *grouping* **and** `SUM`ing on the same column? If you `GROUP BY` the column `Amount` and `OrderDate` you get one row per distinct value of `OrderDate` and `Amount`. – Thom A Jun 26 '23 at 10:35
  • Huh, I *did* [literally ask someone this 2 days ago](https://stackoverflow.com/questions/76541779/sql-grouping-by-id-is-not-giving-unique-results#comment134955751_76541779). – Thom A Jun 26 '23 at 10:44
  • @ThomA if l don't group OrderDate and Amount it produces an error that a group clause should be applied. Can you show me an alternative way? – User_K47 Jun 26 '23 at 14:09

0 Answers0