调整我的SQL查询以允许不同列中的文档类型

时间:2014-05-26 13:28:23

标签: sql sql-server sql-server-2008

请参阅下面的SQL Query,我希望在有逗号的时候拆分Document_Type列。

 DECLARE @t TABLE(Company int,Date_Of_Birth DATETIME,Department VARCHAR(50)
,Document_Type VARCHAR(50), Name VARCHAR(50), Surname VARCHAR(50)
,ID_Number VARCHAR(50), Passport_No VARCHAR(50), Job_Title VARCHAR(50)
,Start_Date DATE,End_Date DATE, Type VARCHAR(50)
)
insert @t
SELECT 302,NULL,605,'New Employee Form','Richard', 'Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Personal Paticulars Form','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Advance','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Employment Agreement Signed','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Inteview Record','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'leave Application','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Doctor Certificate','Richard','Watts',51086,0,NULL,NULL,NULL,NULL union all
SELECT 302,NULL,605,'Miscellaneous Documents','Richard','Watts',51086,0,NULL,NULL,NULL,NULL

--Query ','+ ('')), 1,1,
SELECT DISTINCT ID_Number, Company, Date_Of_Birth,Department ,Name ,
Surname ,Passport_No, Job_Title,Start_Date,
End_Date ,Type, STUFF((SELECT ','+ Document_Type FROM @t WHERE ID_Number = Tmp.ID_Number FOR XML PATH ('')), 1,1,'')
FROM @t Tmp

这是结果。

7504145051086 | 302 | NULL | 605 | Richard | Watts | 0 | NULL | NULL | NULL | NULL |新员工表格,个人资料表格,预先,雇佣协议签署,面试记录,请假申请,医生证明,杂项文件

我正在寻找的是让它像这样运行。 7504145051086 | 302 | NULL | 605 | Richard | Watts | 0 | NULL | NULL | NULL | NULL |新员工表格|个人资料表格|预先|签署就业协议|面试记录|离开申请|医生证书|杂项文件...... 。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT ID_Number, 
Company, 
Date_Of_Birth,
Department ,
Name ,
Surname ,
Passport_No, 
Job_Title,
Start_Date,
End_Date ,
Type, 
STUFF((SELECT '|'+ Document_Type 
       FROM @t 
       WHERE ID_Number = Tmp.ID_Number 
       FOR XML PATH ('')), 1,1,'')
FROM @t Tmp

答案 1 :(得分:0)

要将文档类型作为字段而不是行,您需要使用PIVOT。 这里的要点是,没有一个字段可用于获取透视字段的名称,因此我们必须创建

With N AS ( 
  SELECT *
       , Pvt_Col = 'Document_Type_' 
       + CAST(Row_Number() OVER (PARTITION BY ID_Number 
                                 ORDER BY Document_Type) as VARCHAR) 
  FROM @t ) 

如果知道不同的值并且很少可以使用CASE

伪造它
With N AS ( 
  SELECT *
       , Pvt_Col = 'Document_Type_' 
       + CAST(Row_Number() OVER (PARTITION BY ID_Number 
                                 ORDER BY Document_Type) as VARCHAR) 
  FROM @t ) 
SELECT Company, Date_Of_Birth, Department, Document_Type, Name
     , Surname, ID_Number, Passport_No, Job_Title, Start_Date
     , End_Date, Type
     , Document_Type_1 = MAX(CASE WHEN Pvt_Col = 'Document_Type_1' 
                                  THEN Document_Type
                                  ELSE NULL
                             END)
     , Document_Type_2 = MAX(CASE WHEN Pvt_Col = 'Document_Type_2' 
                                  THEN Document_Type
                                  ELSE NULL
                             END)
...
FROM   N
GROUP BY Company, Date_Of_Birth, Department, Document_Type, Name
       , Surname, ID_Number, Passport_No, Job_Title, Start_Date
       , End_Date, Type

如果不同的值更多未知,我们必须使用metascripting,例如创建一个脚本来创建要执行的脚本,这种特殊类型的元脚本称为动态轴

Declare @q varchar(MAX)

With N AS (
  Select *
       , Pvt_Col = 'Document_Type_' 
       + Cast(Row_Number() OVER (PARTITION BY ID_Number ORDER BY Document_Type) as varchar)
  From   @t
), C (Cols) As (
  Select STUFF((SELECT distinct ',' + QUOTENAME(Pvt_Col) 
                FROM   N
                FOR XML PATH(''), TYPE
               ).value('.', 'NVARCHAR(MAX)') 
              , 1, 1, '')
)
Select @q 
       = 'With N AS (
          SELECT *, Pvt_Col = ''Document_Type_'' 
               + Cast(Row_Number() OVER (PARTITION BY ID_Number ORDER BY Document_Type) as varchar)
          FROM   @t
        )           
        SELECT ID_Number, Company, Date_Of_Birth, Department, Name
             , Surname, Passport_No, Job_Title, Start_Date
             , End_Date, Type
             , ' + Cols + ' 
        FROM  (SELECT Company, Date_Of_Birth, Department, Document_Type, Name
                    , Surname, ID_Number, Passport_No, Job_Title, Start_Date
                    , End_Date, Type, Pvt_Col
               FROM N
           ) x
           PIVOT 
           (max(Document_Type) for Pvt_Col in (' + Cols + ')) pvt'
From C

execute(@q)