3

can we pivot rows to multiple columns i.e

enter image description here

Create table #Temp_Trans    

(   
P_ID int,   
Custom_Name varchar(30),    
Text_Value varchar(30), 
Number_Value int,   
[DateTime] datetime,    

)   

insert into #Temp_Trans values  
(1111,'DepartmentCode','AAA',null,null),    
(1111,'Year','2017',null,null), 
(1111,'StartDate',null,null,'2002-10-02'),  
(1111,'EmpID',null,555,null),   
(1111,'EmpTitle','TeamLeader',null,null),   

(2222,'DepartmentCode','BBB',null,null),    
(2222,'Year','2016',null,null), 
(2222,'StartDate',null,null,'2010-10-02'),  
(2222,'EmpID',null,null,null),  
(2222,'EmpTitle',null,null,null),   

(3333,'DepartmentCode','CCC',null,null),    
(3333,'Year','2017',null,null), 
(3333,'StartDate',null,null,'2017-10-02')   

select * from #Temp_Trans

http://sqlfiddle.com/#!6/d4eb9

or any-other way. Most records (p_id) will have fixed number of columns (Custom Name) heading - few some and some none. Many thanks

0

2 Answers 2

3

Try this. I'd suggest reading up on pivots on your own since they're a little bit funky. You can do that here: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

The one thing you'll notice is I coalesced all the columns into a single data type (string), because trying to do it across multiple columns is a nightmare. If you still need to enforce the data types, I'd do that in the final select.

select 
    p_id,
    DepartmentCode = cast(DepartmentCode as varchar(30)),
    Year = cast(Year as int),
    StartDate = cast(StartDate as datetime),
    EmpId = cast(EmpId as int),
    EmpTitle = cast(EmpTitle as varchar(30))
from (select 
          P_ID,
          custom_name,
          Value = coalesce(text_value, cast(number_value as varchar(30)), convert(varchar(30), datetime, 120))
      from #Temp_Trans) s
pivot(max(Value) for custom_name in (DepartmentCode, Year, StartDate, EmpID,EmpTitle))p 

if for some reason you're hellbent on pivoting multiple columns, you'll have to do multiple pivots.

4
  • I've never seen a hellbent Rosebud do multiple pivots (funky). I will definitely check back on this post.
    – Smandoli
    Jan 30, 2017 at 22:08
  • Many thanks Xedni will test on SQL fiddle once i get it working. Also will read upon the link.
    – Rosebud
    Jan 30, 2017 at 22:17
  • 1
    Thanks Xedni it works well. Just one thing some of the Number_Val have number 0 (zero) data i.e StartDate Null,0, 2010-10-02 returns 0. Can we skip 0 zero values...and return the date...on the coalesce
    – Rosebud
    Jan 31, 2017 at 20:14
  • So if it's as simple as that rule alone, it would suffice to just change the order of the columns in the coalesce() statement. coalesce just picks the first non-null value it finds. If Datetime should supercede number_value, that will do the trick. If there are other cases you need to account for, and you, say, need to split out that 0 number value into its own distinct row, that's where it'd get messy.
    – Xedni
    Jan 31, 2017 at 21:14
3

Two quick options:

1) Conditional Aggregation

select P_ID
      ,DepartmentCode = max(case when Custom_Name='DepartmentCode' then Text_Value end) 
      ,Year           = max(case when Custom_Name='Year'           then Text_Value end) 
      ,StartDate      = max(case when Custom_Name='StartDate'      then DateTime   end) 
      ,EmpID          = max(case when Custom_Name='EmpID'          then Number_Value end) 
      ,EmpTitle       = max(case when Custom_Name='EmpTitle'       then Text_Value end) 
from #Temp_Trans
Group By P_ID

2) Dynamic Pivot

Declare @SQL varchar(max) 
Select  @SQL = Stuff((Select Distinct ',' + QuoteName(Custom_Name) From #Temp_Trans For XML Path('')),1,1,'')   
Select  @SQL = 'Select P_ID,' + @SQL + ' 
                From (
                       Select P_ID
                             ,ITEM  = Custom_Name
                             ,Value = concat(Text_Value,Number_Value,format(DateTime,''yyyy-MM-dd''))
                       From  #Temp_Trans
                     ) A
                Pivot (max(Value) For Item in (' + @SQL + ') ) p'
Exec(@SQL);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.