Hi Experts ,
Happy New Year!
The below script works fine dynamically transpose the data , I have a requirement to add two columns and get sum of Data1 columns in the last available column Same as for Data2 the result are in screenshot. May be from group by rollup i dont know how to do it.
Secondly i am trying to run a stored procedure by providing the @Startdate ( 10-Oct-2021) and @Endate (11-Oc-2021) , but the script retrieve data all available rows in the database not between parameters passing, please advise what i am doing wrong.
Current results
Required results.
IF OBJECT_ID('tempdb..##Table') IS NOT NULL DROP TABLE [##Table]
GO
SELECT * INTO [##Table] FROM (SELECT CAST('10-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName1' AS VARCHAR(1000)) AS [ItemName], CAST('1336.9' AS FLOAT) AS [Data1], CAST('0.85623' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('10-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName2' AS VARCHAR(1000)) AS [ItemName], CAST('318.62' AS FLOAT) AS [Data1], CAST('0.31913' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('10-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName3' AS VARCHAR(1000)) AS [ItemName], CAST('40' AS FLOAT) AS [Data1], CAST('50' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('10-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName4' AS VARCHAR(1000)) AS [ItemName], CAST('1068.51' AS FLOAT) AS [Data1], CAST('7.23771' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('10-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName5' AS VARCHAR(1000)) AS [ItemName], CAST('238.51' AS FLOAT) AS [Data1], CAST('3.56735' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('11-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName1' AS VARCHAR(1000)) AS [ItemName], CAST('10' AS FLOAT) AS [Data1], CAST('20' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('11-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName2' AS VARCHAR(1000)) AS [ItemName], CAST('11' AS FLOAT) AS [Data1], CAST('21' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('11-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName3' AS VARCHAR(1000)) AS [ItemName], CAST('12' AS FLOAT) AS [Data1], CAST('22' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('11-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName4' AS VARCHAR(1000)) AS [ItemName], CAST('13' AS FLOAT) AS [Data1], CAST('23' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('11-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName5' AS VARCHAR(1000)) AS [ItemName], CAST('14' AS FLOAT) AS [Data1], CAST('24' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('12-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName1' AS VARCHAR(1000)) AS [ItemName], CAST('10' AS FLOAT) AS [Data1], CAST('20' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('12-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName2' AS VARCHAR(1000)) AS [ItemName], CAST('11' AS FLOAT) AS [Data1], CAST('21' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('12-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName3' AS VARCHAR(1000)) AS [ItemName], CAST('12' AS FLOAT) AS [Data1], CAST('22' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('12-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName4' AS VARCHAR(1000)) AS [ItemName], CAST('13' AS FLOAT) AS [Data1], CAST('23' AS FLOAT) AS [Data2] UNION ALL
SELECT CAST('12-Oct-2021 00:00:00' AS DATETIME) AS [Startdate], CAST('ItemName5' AS VARCHAR(1000)) AS [ItemName], CAST('14' AS FLOAT) AS [Data1], CAST('24' AS FLOAT) AS [Data2] ) a
Declare @sql nvarchar(max)
Declare @StartDate DATETIME
Declare @EndDate DATETIME
Set @StartDate = '10-OCT-2021'
Set @EndDate = '11-OCT-2021'
select @sql = isnull(@sql + ',', N'Select [Startdate],') + char(13)
+ N' [ItemName] = max(Case when [ItemName] = ''' + [ItemName] + ''' THEN [ItemName] end),' + char(13)
+ N' [Data1] = max(Case when [ItemName] = ''' + [ItemName] + ''' THEN [Data1] end),' + char(13)
+ N' [Data2] = max(Case when [ItemName] = ''' + [ItemName] + ''' THEN [Data2] end)' + char(13)
from ##Table where Startdate>=@StartDate and Startdate<=@Enddate
group by [ItemName]
order by [ItemName]
select @sql = @sql
+ N'from [##Table]' + char(13)
+ N'group by [Startdate]'
print @sql
exec sp_executesql @sql