2012-11-28 63 views
0

這是我的查詢行轉換成列

select * from dbo.tblHRIS_ChildDetails where intSID=463 

輸出:

intCHID intsid nvrchildname nvrgender  dttchildDOB     Occupation 
     3  463  SK   Female  2001-12-11 00:00:00.000 Studying  
     4  463  SM   Male  2007-10-08 00:00:00.000 Student 

我需要這樣這樣輸出的查詢是動態的,它可以返回基於intSID

行的n個
chidname1 gender DOB childoccupation1   chidname2 gender DOB childoccupation2 
    SK  female 2001-12-11 00:00:00.000 studying  SM  Male 2007-10-08 00:00:00.000  Student 
+0

這是什麼RDBMS? –

+0

Sql server 2005 –

+0

使用關係模型時,你所要求的通常不會完成。你能告訴我們爲什麼你想要(或想你想要)這樣做嗎? – jimhark

回答

0

你必須提供不同的列名稱,但除此之外,它很簡單。但正如其他人所說,這不是通常的做法,看起來像是如果你想用兩列打印報告。

select 
    max(case when intCHID=1 then nvrchildname end) as chidname1, 
    max(case when intCHID=1 then gender  end) as gender1, 
    max(case when intCHID=1 then dttchildDOB end) as DOB1, 
    max(case when intCHID=1 then Occupation end) as cildOccupation1, 
    max(case when intCHID=2 then nvrchildname end) as chidname2, 
    max(case when intCHID=2 then gender  end) as gender2, 
    max(case when intCHID=2 then dttchildDOB end) as DOB2, 
    max(case when intCHID=2 then Occupation end) as cildOccupation2 
from 
    dbo.tblHRIS_ChildDetails 
where 
    intSID=463 
+0

我不能通過這intCHID = 1這是自動生成的列 –

2

對於這種類型的數據,您需要同時實現UNPIVOT,然後PIVOT的SQL Server的功能。 UNPIVOT從多列中提取數據並將其放在兩列中,然後應用PIVOT將數據轉換回列。

如果你知道所有你想要轉換的值,那麼你可以硬編碼,與此類似:

select * 
from 
(
    select value, col+'_'+cast(rn as varchar(10)) col 
    from 
    (
    select nvrchildname, 
     nvrgender, 
     convert(varchar(10), dttchildDOB, 120) dttchildDOB, 
     occupation, 
     row_number() over(partition by intsid order by intCHID) rn 
    from tblHRIS_ChildDetails 
    where intsid = 463 
) src 
    unpivot 
    (
    value 
    for col in (nvrchildname, nvrgender, dttchildDOB, occupation) 
) unpiv 
) src1 
pivot 
(
    max(value) 
    for col in ([nvrchildname_1], [nvrgender_1], 
       [dttchildDOB_1], [occupation_1], 
       [nvrchildname_2], [nvrgender_2], 
       [dttchildDOB_2], [occupation_2]) 
) piv 

SQL Fiddle with Demo

現在,如果你有一個未知號碼值進行改造,那麼你可以使用動態SQL這樣的:

DECLARE @colsUnpivot AS NVARCHAR(MAX), 
    @query AS NVARCHAR(MAX), 
    @colsPivot as NVARCHAR(MAX) 

select @colsUnpivot = stuff((select ','+quotename(C.name) 
     from sys.columns as C 
     where C.object_id = object_id('tblHRIS_ChildDetails') and 
       C.name not in ('intCHID', 'intsid') 
     for xml path('')), 1, 1, '') 

select @colsPivot = STUFF((SELECT ',' 
         + quotename(c.name 
         +'_'+ cast(t.rn as varchar(10))) 
        from 
        (
         select row_number() over(partition by intsid order by intCHID) rn 
         from tblHRIS_ChildDetails 
        ) t 
        cross apply sys.columns as C 
        where C.object_id = object_id('tblHRIS_ChildDetails') and 
         C.name not in ('intCHID', 'intsid') 
        group by c.name, t.rn 
        order by t.rn 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

set @query 
    = 'select * 
     from 
     (
     select col+''_''+cast(rn as varchar(10)) col, value 
     from 
     (
      select nvrchildname, 
      nvrgender, 
      convert(varchar(10), dttchildDOB, 120) dttchildDOB, 
      occupation, 
      row_number() over(partition by intsid order by intCHID) rn 
      from tblHRIS_ChildDetails 
      where intsid = 463 
     ) x 
     unpivot 
     (
      value 
      for col in ('+ @colsunpivot +') 
     ) u 
    ) x1 
     pivot 
     (
     max(value) 
     for col in ('+ @colspivot +') 
    ) p' 

exec(@query) 

SQL Fiddle with Demo

這兩個查詢的結果是:

| NVRCHILDNAME_1 | NVRGENDER_1 | DTTCHILDDOB_1 | OCCUPATION_1 | NVRCHILDNAME_2 | NVRGENDER_2 | DTTCHILDDOB_2 | OCCUPATION_2 | 
----------------------------------------------------------------------------------------------------------------------------- 
|    SK |  Female | 2001-12-11 |  Studying |    SM |  Male | 2007-10-08 |  Student | 
+0

我有一些疑問,你的解決方案是一流的,但我有性別,職業,childname作爲nvarchar,所以如果運行查詢我得到一些錯誤有這個小提琴的外觀PLZ http://sqlfiddle.com/#!3/164b7/1 –

+0

你可以檢查這一點,並告訴 –

+0

你看到了小提琴:) –