简体   繁体   English

SQL枢轴行到多个列

[英]sql pivot rows to multiple columns

can we pivot rows to multiple columns ie 我们可以将行旋转到多列,即

在此处输入图片说明

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 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. 大多数记录(p_id)的列数(自定义名称)标题都是固定的-很少,有些则没有。 Many thanks 非常感谢

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 您可以在此处执行以下操作: https : //technet.microsoft.com/zh-cn/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. 如果出于某种原因您想对多个列进行透视,则必须进行多个透视。

Two quick options: 两种快速选择:

1) Conditional Aggregation 1)有条件的聚合

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 2)动态枢轴

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM