sql server - How to do Pivoting for the below query? -


i have record set under

agreementid     feedbackdate            dispositioncode 0003sbml00151   2017-03-08 00:00:00.000 ptp 0004sbhl00705   2017-03-17 00:00:00.000 bptp 0007sbml01987   null                    null 0026mss00108    2017-05-20 00:00:00.000 ptp 0026mss00108    2017-03-22 00:00:00.000 ptp 0026mss00108    2016-12-30 00:00:00.000 bptp 0026mss00108    2016-12-29 00:00:00.000 bptp 0026mss00108    2016-12-28 00:00:00.000 bptp 0037sbhl02361   null                    null 0038sbml00291   2017-05-04 00:00:00.000 ptp 0038sbml00291   2017-04-24 00:00:00.000 bptp 0038sbml00291   2017-04-11 00:00:00.000 nc 0038sbml00291   2016-12-22 00:00:00.000 ptp 0038sbml00291   2016-12-09 00:00:00.000 dc 

the desired output be

agreementid     l1      l2      l3      l4      l5 0003sbml00151   ptp     null    null    null    null 0004sbhl00705   bptp    null    null    null    null 0007sbml01987   null    null    null    null    null 0026mss00108    ptp     ptp     bptp    bptp    bptp 0037sbhl02361   null    null    null    null    null 0038sbml00291   ptp     bptp    nc      ptp     dc 

sql schema

declare @t table(agreementid varchar(50),feedbackdate varchar(50),dispositioncode varchar(10)) insert @t  select '0003sbml00151','2017-03-08 00:00:00.000','ptp' union select '0004sbhl00705','2017-03-17 00:00:00.000','bptp' union select '0007sbml01987',null,null union select '0026mss00108','2017-05-20 00:00:00.000','ptp' union select '0026mss00108','2017-03-22 00:00:00.000','ptp' union select '0026mss00108','2016-12-30 00:00:00.000','bptp' union select '0026mss00108','2016-12-29 00:00:00.000','bptp' union select '0026mss00108','2016-12-28 00:00:00.000','bptp' union select '0037sbhl02361',null,null union select '0038sbml00291','2017-05-04 00:00:00.000','ptp' union select '0038sbml00291','2017-04-24 00:00:00.000','bptp' union select '0038sbml00291','2017-04-11 00:00:00.000','nc' union select '0038sbml00291','2016-12-22 00:00:00.000','ptp' union select '0038sbml00291','2016-12-09 00:00:00.000','dc'  select * @t 

here attempt

;with cte1 as(  select agreementid,  abc = stuff(              (select '.' + dispositioncode                @t t1               t1.agreementid = t2.agreementid --and t1.rn = t2.rn               xml path (''))              , 1, 1, '') @t t2 group agreementid)  --select * --from cte1 ,cte2 as(  select agreementid,      x= iif(charindex('.',abc,1) null,'null value',iif(charindex('.',abc,1) = 0,'single value','multiple value'))          --,col1 = substring(abc,1,iif(charindex('.',abc,1) = null,0,iif(charindex('.',abc,1) = 0,len(abc),(charindex('.',abc,1)-1))))         --,charindex('.',abc,1)         ,abc cte1) ,cte3 as( select      agreementid     ,col1 =iif(x = 'null value', null,iif(x='single value',substring(abc,1,len(abc)),substring(abc,1,(charindex('.',abc,1)-1))))     ,abc     ,othercols = iif(x = 'multiple value',substring(abc,charindex('.',abc,1)+1,len(abc)),'') cte2)  select      agreementid      ,l1 = iif(col1 null, '--',col1)     ,l2 = iif(parsename(othercols,4)is null, '--',parsename(othercols,4))     ,l3 = iif(parsename(othercols,3)is null, '--',parsename(othercols,3))     ,l4 = iif(parsename(othercols,2)is null, '--',parsename(othercols,2))     ,l5 = iif(parsename(othercols,1)is null, '--',parsename(othercols,1))    cte3 

disadvantages

a) slow query

b) failed below case

agreementid      feedbackdate           dispositioncode 0002sbml01241   2017-05-04 00:00:00.000 today 0002sbml01241   2017-04-24 00:00:00.000 ptp 0002sbml01241   2017-04-11 00:00:00.000 ptp 0002sbml01241   2016-12-22 00:00:00.000 ptp 

actual


agreementid     l1      l2  l3  l4  l5 0002sbml01241   today   --  ptp ptp ptp 

expected


 agreementid     l1     l2  l3  l4  l5 0002sbml01241   today   ptp ptp ptp -- 

thanks support in advance.

this in case dynamic levels:

declare @t table(agreementid varchar(50),feedbackdate varchar(50),dispositioncode varchar(10)) insert @t  select '0003sbml00151','2017-03-08 00:00:00.000','ptp' union select '0004sbhl00705','2017-03-17 00:00:00.000','bptp' union select '0007sbml01987',null,null union select '0026mss00108','2017-05-20 00:00:00.000','ptp' union select '0026mss00108','2017-03-22 00:00:00.000','ptp' union select '0026mss00108','2016-12-30 00:00:00.000','bptp' union select '0026mss00108','2016-12-29 00:00:00.000','bptp' union select '0026mss00108','2016-12-28 00:00:00.000','bptp' union select '0037sbhl02361',null,null union select '0038sbml00291','2017-05-04 00:00:00.000','ptp' union select '0038sbml00291','2017-04-24 00:00:00.000','bptp' union select '0038sbml00291','2017-04-11 00:00:00.000','nc' union select '0038sbml00291','2016-12-22 00:00:00.000','ptp' union select '0038sbml00291','2016-12-09 00:00:00.000','dc'  select   'l'+convert(varchar(255),row_number()over(partition agreementid order agreementid))'rno',* test @t order agreementid  declare @levels nvarchar(max),@sql nvarchar(max)  select @levels= stuff((select distinct ','+rno test  xml path('')),1,1,'')  set @sql= 'select agreementid,'+@levels+' (select agreementid,dispositioncode,rno test )as temp'+' pivot'+' (max(dispositioncode) rno in ('+@levels+') )as pvt'  exec sp_executesql @sql 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -