Ahmad saleh
Ahmad saleh

Reputation: 103

Row summation based on dates

I have a table as the following: enter image description here What I am trying to do is to get the sum of values in the createdYesterday column based on the month, for example for May or 05 month I need to get the some of all rows between 5-1-2018 to 5-30-2018 and write this value to a variable to a table.

The final result should be a table that has the 12 months and the sum of CreatedYesterday in each month.

month    |  Sum

   Jan   |  100

   Feb   |   500

   Mar   | 1000

Upvotes: 0

Views: 26

Answers (1)

Ian-Fogelman
Ian-Fogelman

Reputation: 1605

   CREATE TABLE #TEMP_A
(
CREATEDYESTERDAY INT,
CREATEDTHISWEEK INT,
CREATEDTHISYEAR INT,
CALCDATE DATE


)


INSERT INTO #TEMP_A
VALUES(0,0,26226,'05/18/2018')
INSERT INTO #TEMP_A
VALUES(71,1647,7402,'05/18/2018')
INSERT INTO #TEMP_A
VALUES(21,60,1931,'05/18/2018')
INSERT INTO #TEMP_A
VALUES(21,60,1931,'04/18/2018')

SELECT SUM(CREATEDYESTERDAY) AS [SUM],DATENAME(MONTH,CALCDATE) AS [MONTH] FROM #TEMP_A GROUP BY DATENAME(MONTH,CALCDATE)

enter image description here

Upvotes: 1

Related Questions