ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Fetch data Month Wise using Dynamic PIVOT query in SQL Server<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample query.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">CREATE TABLE #ItemMasterFile(ItemCode INT,Descriptionitem VARCHAR(50),SecID INT,weight int); CREATE TABLE #Probale(BID INT,ItemCode INT,prdQTY INT,Orderno int,weight int,Entrydate DATETIME,DelID int); INSERT INTO #ItemMasterFile VALUES(1,&#39;A&#39;,1,100) INSERT INTO #ItemMasterFile VALUES(2,&#39;B&#39;,2,100) INSERT INTO #ItemMasterFile VALUES(3,&#39;C&#39;,3,100) INSERT INTO #ItemMasterFile VALUES(4,&#39;D&#39;,4,100) INSERT INTO #ItemMasterFile VALUES(5,&#39;e&#39;,5,100) INSERT INTO #ItemMasterFile VALUES(6,&#39;f&#39;,6,100) INSERT INTO #ItemMasterFile VALUES(7,&#39;g&#39;,4,100) INSERT INTO #ItemMasterFile VALUES(8,&#39;h&#39;,4,100) INSERT INTO #ItemMasterFile VALUES(9,&#39;K&#39;,2,100) INSERT INTO #ItemMasterFile VALUES(10,&#39;L&#39;,4,100) INSERT INTO #ItemMasterFile VALUES(11,&#39;M&#39;,2,100); INSERT INTO #Probale VALUES(1,1,1,001,100,&#39;01-05-2019&#39;,null) INSERT INTO #Probale VALUES(1,1,1,001,100,&#39;01-06-2019&#39;,null) INSERT INTO #Probale VALUES(2,3,1,001,200,&#39;02-07-2019&#39;,null) INSERT INTO #Probale VALUES(3,11,1,002,200,&#39;03-08-2019&#39;,null) INSERT INTO #Probale VALUES(4,10,1,002,200,&#39;08-08-2019&#39;,null) INSERT INTO #Probale VALUES(4,1,1,003,200,&#39;08-08-2019&#39;,null) INSERT INTO #Probale VALUES(4,1,1,003,200,&#39;08-08-2019&#39;,null) DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX) SELECT @ColumnName= ISNULL(@ColumnName + &#39;,&#39;,&#39;&#39;)+ QUOTENAME(Entrydate) FROM (SELECT DISTINCT Entrydate FROM #Probale) AS Entrydate SET @DynamicPivotQuery = N&#39;SELECT * FROM ( SELECT t.ItemCode,t.Descriptionitem,t2.prdQTY,t2.Entrydate FROM #ItemMasterFile t FULL OUTER JOIN #Probale t2 ON t.ItemCode = t2.ItemCode ) p PIVOT(Max(prdQTY) FOR Entrydate IN (&#39; +@ColumnName+ &#39;)) AS pvt&#39; EXEC (@DynamicPivotQuery) DROP TABLE #ItemMasterFile DROP TABLE #Probale</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/126375/Fetch-data-Month-Wise-using-Dynamic-PIVOT-query-in-SQL-Server/https://www.aspforums.net:443/Threads/126375/Fetch-data-Month-Wise-using-Dynamic-PIVOT-query-in-SQL-Server/Fri, 02 Aug 2019 04:37:26 GMT