Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys i have question i have tried so many things serached in google found somthing but not for me so the question is that i want to make select that will get sum of values and get it monthly that is more than
SQL
@veriable(data)
value about 12 month only soo i have written some code but not working for me , plz help me out someone

What I have tried:

DECLARE @cols AS NVARCHAR(MAX)
DECLARE @cols2 AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)
DECLARE @year DATE

SET @year='2017-04-01 00:00:00.000'
SELECT @cols = STUFF(
                        (   SELECT DISTINCT ',' + CONVERT(NVARCHAR(20),DATENAME(MONTH, Date))
 FROM dbo.Reestr LEFT JOIN dbo.Source AS j ON Sender_Id=j.ID 
WHERE (Date BETWEEN @year and dateadd(month,12,@year ))

 AND (Op_Type_Id=1 OR Op_Type_Id=3 OR Op_Type_Id=5)
GROUP BY SOURCE,DATENAME(MONTH, Date)	 
                            FOR xml path('')
                        )
                        , 1
                        , 1
                        , '')

						SELECT @cols
SELECT @query = 
'DECLARE @year DATE

SET @year=''2017-04-01 00:00:00.000''
SELECT *
FROM (SELECT   j.Source,
       DATENAME(MONTH, Date) [Month],
     ISNULL(SUM(Value), 0) AS value
      FROM dbo.Reestr LEFT JOIN dbo.Source AS j ON Sender_Id=j.ID WHERE 
	  (Date BETWEEN @year and dateadd(month,12,@year ))
	   AND (Op_Type_Id=1 OR Op_Type_Id=3 OR Op_Type_Id=5)
	   GROUP BY SOURCE,Value,
	 DATENAME(MONTH, Date)) mon 
PIVOT 
(
    sum(value)
    FOR [Month] IN  (' + @cols + ')) Piv'



EXEC SP_EXECUTESQL @query
Posted
Updated 6-Mar-18 23:24pm
Comments
Maciej Los 7-Mar-18 4:42am    
What database engine?
GTR0123 7-Mar-18 4:44am    
Sql Server 2012

1 solution

Check this:
SQL
--define fiscal year start and end
DECLARE @fiscalYearStart DATE = CONVERT(DATE, '2017-04-01')
DECLARE @fiscalYearEnd DATE = DATEADD(MM, 12, @fiscalYearStart)
--variable to store column names: [2017-04-01],[2017-05-01],[2017-06-01],...
DECLARE @cols NVARCHAR(2000) = N'';
--recursive query to produce column names
;WITH Dates AS
(
	SELECT @fiscalYearStart AS CommonDate
	UNION ALL
	SELECT DATEADD(MM, 1, CommonDate) AS CommonDate
	FROM Dates
	WHERE DATEADD(MM, 1, CommonDate)<@fiscalYearEnd
)
SELECT @cols = STUFF((SELECT '],[' + CONVERT(NVARCHAR(10),CommonDate) 
	FROM Dates
	FOR XML PATH('')), 1, 2, '') + ']'

--variable to store pivot query
DECLARE @qry NVARCHAR(MAX) = N'SELECT ' + @cols + 
    ' FROM (' + 
    '    SELECT j.[Value], CAST(DATEADD(DAY,-DAY(j.[Date])+1, CAST(j.[Date] AS DATE)) AS DATE) AS CommonMonthDate' + 
    '    FROM FROM dbo.Reestr AS r LEFT JOIN dbo.Source AS j ON r.Sender_Id=j.ID' +
    '    WHERE r.Op_Type_Id IN(1, 3, 5)' +
    ') AS DT' +
    'PIVOT(SUM(Value) FOR CommonMonthDate IN(' + @cols + ')) AS PVT'

EXEC(@qry)


Note: Please, change table aliases (j or r) respectively!

I did use smart trick from: sqlauthority.com: SQL SERVER – Script to Find First Day of Current Month[^] to convert each date into common form: first day in month to bea able to pivot data in monthly manner.
 
Share this answer
 
Comments
GTR0123 7-Mar-18 6:14am    
Ty Ty this helps ty again
Maciej Los 7-Mar-18 7:17am    
You're very welcome.
You can say "Thank you" through the voting system, by giving me stars (1-poor answer, 5-excellent) in the right-top corner of my answer.
Karthik_Mahalingam 8-Mar-18 3:40am    
5
Maciej Los 8-Mar-18 3:48am    
Thank you, Karthik.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900