sql server - TSQL Pivot query -
i need on tsql pivot , getting error. appreciated. below query , data.
declare @cols varchar(8000), @query varchar(8000) set @cols = stuff((select distinct ',' + quotename(c.steps) tableabc c xml path(''), type ).value('.', 'varchar(8000)') ,1,1,'') --print @cols set @query = 'select name, elapsedtime_sec,' + @cols + ' ( select name,elapsedtime_sec tableabc ) x pivot ( max(steps) elapsedtime_sec in (' + @cols + ') ) p ' execute(@query)
table information:
without expected output , faulty pivot query, i'm guessing want. here script works based loosely on question. i'm guessing it's not output want, can work script , maybe turn need.
create table #tableabc(id int,name varchar(256), steps varchar(1024),elapsedtime_sec int); insert #tableabc(id,name,steps,elapsedtime_sec)values (2,'features','touch/stain',25), (29,'shipping','step requiring qa',129), (47,'behavioral','gloves',8); declare @cols varchar(8000), @query varchar(8000); set @cols = stuff((select distinct ',' + quotename(c.steps) #tableabc c xml path(''), type ).value('.', 'varchar(8000)') ,1,1,'') set @query = 'select * #tableabc pivot ( max(elapsedtime_sec) steps in (' + @cols + ') ) p '; execute(@query); drop table #tableabc;
output:
+----+------------+--------+-------------------+-------------+ | id | name | gloves | step requiring qa | touch/stain | +----+------------+--------+-------------------+-------------+ | 47 | behavioral | 8 | null | null | | 2 | features | null | null | 25 | | 29 | shipping | null | 129 | null | +----+------------+--------+-------------------+-------------+
Comments
Post a Comment