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:

table info

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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -