2016-02-24 48 views
0

我有這個表「概述」SQL如何轉列行

+--------+---------+-------+-------+-----+-----+----+--------+ 
|fName |shName |project|task |owner|hours|cmpt|modified| 
+--------+---------+-------+-------+-----+-----+----+--------+ 
|Taskday1|IBM  |Website|develop|sam | 5 |25 |2/2/2016 
|Taskday1|IBM  |website|test |sam | 7 |20 |2/2/2016 
|Taskday1|IBM  |support|design |ivan | 2 |7 |2/2/2016    
|Taskday2|DELL  |android|config |peter| 9 |30 |3/2/2016    
|Taskday2|IBM  |Website|develop|sam | 9 |45 |3/2/2016    

我想tranpsose列小時,CMPT,修改和重複每行3次,我的表將這個樣子

|fName |shName|project|task |owner| h/c/m |val 
+--------+------+-------+-------+-----+--------+----------+ 
|Taskday1|IBM |Website|develop|sam |hours |5 
|Taskday1|IBM |Website|develop|sam |cmpt |25 
|Taskday1|IBM |Website|develop|sam |modified|2/2/2016    
|Taskday1|IBM |website|test |sam |hours |7   
|Taskday1|IBM |website|test |sam |cmpt |20    
|Taskday1|IBM |website|test |sam |modified|2/2/2016 
|Taskday1|IBM |support|design |ivan |hours |2 
|Taskday1|IBM |support|design |ivan |cmpt |7 
|Taskday1|IBM |support|design |ivan |modified|2/2/2016 
|Taskday2|DELL |android|config |peter|hours |9 
|Taskday2|DELL |android|config |peter|cmpt |30 
|Taskday2|DELL |android|config |peter|modified|3/2/2016 
|Taskday2|IBM |Website|develop|sam |hours |9 
|Taskday2|IBM |Website|develop|sam |cmpt |45 
|Taskday2|IBM |Website|develop|sam |modified|3/2/2016 

任何想法,請問如何使它?非常感謝你

+0

但是爲什麼?這似乎毫無意義。 – Takarii

+0

做'UNION ALL',每個h/c/m有一個'select' – jarlh

回答

1

可通過unpivoting實現這裏是單向的

SELECT fName, 
     shName, 
     project, 
     task, 
     owner, 
     [h/c/m], 
     value 
FROM yourtable 
     CROSS apply (VALUES ('hours',hours), 
          ('cmpt',cmpt), 
          ('modified',modified)) cs([h/c/m], value) 

注:如果modifiedDate數據類型,那麼你需要castmodified作爲varchar

1

只需使用UNPIVOT功能:

DECLARE @DataSoruce TABLE 
( 
    [fName] VARCHAR(12) 
    ,[shName] VARCHAR(8) 
    ,[proiject] VARCHAR(8) 
    ,[task] VARCHAR(8) 
    ,[owner] VARCHAR(8) 
    ,[hours] TINYINT 
    ,[cmpt] INT 
    ,[modified] VARCHAR(8) 
); 

INSERT INTO @DataSoruce ([fName], [shName], [proiject], [task], [owner], [hours], [cmpt], [modified]) 
VALUES ('Taskday1', 'IBM', 'Website', 'develop', 'sam', '5', '25', '2/2/2016') 
     ,('Taskday1', 'IBM', 'website', 'test', 'sam', '7', '20', '2/2/2016') 
     ,('Taskday1', 'IBM', 'support', 'design ', 'ivan ', '2', '7', '2/2/2016') 
     ,('Taskday2', 'DELL', 'android', 'config ', 'peter', '9', '30', '3/2/2016') 
     ,('Taskday2', 'IBM', 'Website', 'develop', 'sam', '9', '45', '3/2/2016'); 

SELECT [fName] 
     ,[shName] 
     ,[proiject] 
     ,[task] 
     ,[owner] 
     ,[h/c/m] 
     ,[Val] 
FROM 
(
    SELECT [fName] 
      ,[shName] 
      ,[proiject] 
      ,[task] 
      ,[owner] 
      ,CAST([hours] AS VARCHAR(8)) AS [hours] 
      ,CAST([cmpt] AS VARCHAR(8)) AS [cmpt] 
      ,CAST([modified] AS VARCHAR(8)) AS [modified] 
    FROM @DataSoruce 
) DS 
UNPIVOT 
(
    [Val] FOR [h/c/m] IN ([hours], [cmpt], [modified]) 
) UNPVT 

enter image description here

,當你在列就UNPIVOT注意,列必須是同一類型(這就是爲什麼我在鑄造他們先)。