2017-04-27 43 views
1

我有一個展示的項目負責人(RES_)如下表:移調列值成排SQL

ID NUMBER BK no_ Res 
LUC00003  BK001  CLARETH 
LUC00003  BK001  MARC 
LUC00009  BK001  CLARETH 
LUC00009 BK001  MICHAEL 

我想在一行中顯示RES_而不是列,爲了得到每一行對應一個ID,如下圖所示:

ID NUMBER BK no_ Res 1  Res2 
LUC00003  BK001  CLARETH MARC 
LUC00009  BK001  CLARETH MICHAEL 

這裏下面的查詢:

SELECT 
    C.[ID NUMBER], 
    CA.[BK No_], 
    B.[Res] 
FROM [IDList] C 
LEFT JOIN [BK list] CA ON C.[ID NUMBER] = CA.[ID NUMBER] 
LEFT JOIN [Res List] B ON B.[ID NUMBER]= C.[ID NUMBER] AND B.[BK No_]=CA.[BK No_] 
+0

,如果你想這個解決方案,爲任意數量的RES_價值的工作,你不能動態地添加colums。如果您知道Res_將會有一個或兩個值,則可以創建兩列,如果未找到任何結果,那麼可能爲空。然而,你可以將所有結果連接成一個coluimn,例如:http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string – mortb

+0

你需要「Res 1」和「Res 2」作爲附加值PER行,之後您可以使用[PIVOT](https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot) - 函數 – swe

+0

也許這個問題可以幫助你,它的一個可能的重複:http://stackoverflow.com/questions/41768834/sql-pivot-with-multiple-values – swe

回答

1

使用條件aggregatio N:

select 
    id_number 
    , bk_no 
    , Res1 = max(case when rn = 1 then Res end) 
    , Res2 = max(case when rn = 2 then Res end) 
from (
    select * 
    , rn = row_number() over (partition by id_number, bk_no order by res) 
    from t 
) sub 
group by id_number, bk_no 

rextester演示:http://rextester.com/DFCC86645

回報:

+-----------+-------+---------+---------+ 
| id_number | bk_no | Res1 | Res2 | 
+-----------+-------+---------+---------+ 
| LUC00003 | BK001 | CLARETH | MARC | 
| LUC00009 | BK001 | CLARETH | MICHAEL | 
+-----------+-------+---------+---------+ 

動態pivot()版本:

declare @cols nvarchar(max); 
declare @sql nvarchar(max); 

    select @cols = stuff((
    select distinct 
     ',' + quotename('Res' 
      +convert(varchar(10),row_number() over (
       partition by id_number, bk_no 
       order by  res 
     )) 
     ) 
     from t 
     for xml path (''), type).value('.','nvarchar(max)') 
    ,1,1,''); 

select @sql = ' 
select id_number, bk_no, ' + @cols + ' 
    from (
    select 
     id_number 
     , bk_no 
     , Res 
     , rn=''Res'' + convert(varchar(10),row_number() over (
       partition by id_number, bk_no 
       order by  res 
     )) 
     from t 
    ) as a 
pivot (max([Res]) for [rn] in (' + @cols + ')) p'; 
select @sql as CodeGenerated; 
exec sp_executesql @sql; 

回報:

+-----------------------------------------------------------------+ 
|       CodeGenerated       | 
+-----------------------------------------------------------------+ 
|  select id_number, bk_no, [Res1],[Res2]      | 
|  from (             | 
|   select             | 
|    id_number           | 
|   , bk_no            | 
|   , Res             | 
|   , rn='Res' + convert(varchar(10),row_number() over (| 
|     partition by id_number, bk_no     | 
|     order by  res        | 
|    ))            | 
|   from t            | 
|   ) as a            | 
|  pivot (max([Res]) for [rn] in ([Res1],[Res2])) p   | 
+-----------------------------------------------------------------+ 
+-----------+-------+---------+---------+ 
| id_number | bk_no | Res1 | Res2 | 
+-----------+-------+---------+---------+ 
| LUC00003 | BK001 | CLARETH | MARC | 
| LUC00009 | BK001 | CLARETH | MICHAEL | 
+-----------+-------+---------+---------+