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
4

2 に答える 2

2

このタイプのデータの場合、SQL ServerUNPIVOTの機能と機能の両方を実装する必要があります。PIVOTUNPIVOT複数の列からデータを取得して 2 つの列に配置し、 を適用し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 を参照してください

変換する値の数が不明な場合は、動的 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 を参照してください

両方のクエリの結果は次のとおりです。

| 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 |
于 2012-11-28T10:25:25.553 に答える
0

個別の列名を指定する必要がありますが、それ以外は簡単です。しかし、他の人が述べたように、これは一般的に行われておらず、2 列のレポートを印刷したい場合のように見えます。

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
于 2012-11-28T07:08:44.163 に答える