Author |
Topic |
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2013-11-25 : 08:04:37
|
hiI would like to convert dates in rows to columns and the dates are dynamics. Here is my sample data and expected result set. Thanks a lot.Sample DataA 2013-08-12A 2013-08-13A 2013-08-14A 2013-08-15A 2013-08-16B 2013-08-12B 2013-08-13B 2013-08-14B 2013-08-15B 2013-08-16C 2013-08-12C 2013-08-13C 2013-08-14C 2013-08-15C 2013-08-16Expected Results 2013-08-12 2013-08-13 2013-08-14 2013-08-15 2013-08-16ABC |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-25 : 09:34:30
|
something like belowDECLARE @DAteList varchar(5000),@SQL varchar(max)SELECT @DAteList =STUFF ((SELECT DISTINCT ',[' + CONVERT(varchar(100),DatefIeld,112) + ']' FROM Table ORDER BY ',[' + CONVERT(varchar(100),DatefIeld,112) + ']' FOR XML PATH('')),1,1'')SET @SQL='SELECT * FROM Table t PIVOT (MAX(1) FOR DateField IN (' + @DateList + '))p'EXEC(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2013-11-25 : 10:09:54
|
hiMy View_2 consist of 2 fields, CustName and Dates.I tried using thisDECLARE @DAteList varchar(5000), @SQL varchar(max)SELECT @DAteList = STUFF ((SELECT DISTINCT Dates FROM View_2 ORDER BY Dates FOR XML PATH('')),1,1,'')SET @SQL='SELECT * FROM View_2 t PIVOT (MAX(1) FOR Dates IN (' + @DateList + '))p'EXEC(@SQL)I received this error:Msg 102, Level 15, State 1, Line 3Incorrect syntax near '1'.I could not figure out how to solve this error. Thanks a lot. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-25 : 13:30:39
|
Please try to use as suggestedDECLARE @DAteList varchar(5000), @SQL varchar(max)SELECT @DAteList = STUFF ((SELECT DISTINCT ',[' + Dates + ']'FROM View_2ORDER BY DatesFOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM View_2 tPIVOT (MAX(1) FOR Dates IN (' + @DateList + '))p'EXEC(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2013-11-26 : 00:18:40
|
hiI have now add a where clause in both query using @BatchID as a variable which is a GUID in my view_2. the error I got which I think is cause by the Set @SQL for pivot in bold. The is the error message"Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'F74FC'.How should I go about it? Thanks a lotOne more thing, without the where clause everything is fine. Thanks.Here is the Code which I tried executing:DECLARE @DateList varchar(5000), @SQL varchar(max), @batchid uniqueidentifierset @batchid = '460f74fc-c318-4152-b758-4afa1db9c890'SELECT @DateList = STUFF((SELECT DISTINCT ',[' + Dates + ']'FROM View_2 where View_2.BatchID = @batchidORDER BY ',[' + Dates + ']'FOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM View_2 t where t.batchid = '+ cast(@batchid as varchar(100))+ ') PIVOT (Max(empty) FOR Dates IN (' + @DateList + '))p'EXEC(@SQL) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-11-26 : 01:32:06
|
do a PRINT @SQL before the EXEC statement. You will be able to spot the error KH[spoiler]Time is always against us[/spoiler] |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2013-11-26 : 02:29:59
|
Thanks visakh16 and KHTanThe correct one is this:DECLARE @DateList varchar(5000), @SQL varchar(max), @batchid uniqueidentifierset @batchid = '460f74fc-c318-4152-b758-4afa1db9c890'SELECT @DateList = STUFF((SELECT DISTINCT ',[' + Dates + ']'FROM View_2 where View_2.BatchID = @batchidORDER BY ',[' + Dates + ']'FOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM View_2 tPIVOT (Max(t.empty) FOR t.Dates IN (' + @DateList + '))pwhere p.batchid = '''+ cast(@batchid as varchar(100))+ ''''EXEC(@SQL) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-26 : 05:30:10
|
Glad that you got it sorted------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|