2

以下のようなクエリがあり、次の結果が得られます。


クエリ

select OrderID, AccessName, Address1, Postcode, GeneralInstructions, --CompanyName,
col, --col+'_'+cast(rn as varchar(10)) col, 
val
from 
(
   select o.OrderID, js.JobStatusID, p.Name, o.AccessName, o.Address1, o.Postcode,   oj.GeneralInstructions, s.CompanyName
  , row_number() over(partition by o.OrderID 
                      order by js.JobStatusID) rn
FROM 
NEPCCO.Orders o
inner join NEPCCO.Clients c on o.ClientID = c.ClientID
inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
inner join NEPCCO.Products p on oj.ProductID = p.ProductID
inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
where 
o.OrderID in (50211, 44621) and 
(p.ProductID in (35,36,37,38,38,40,41,42,43) or  p.ProductID in (33,34))
 ) d
 unpivot
 (
   val
   for col in (Name)
) un

最初の出力

OrderID     AccessName      Address1                PostCode   Col Val    
44621   Mr Hayden   7 Broad Rush Green  LU7 2XA    Name  FPA - 28
44621   Mr Hayden   7 Broad Rush Green  LU7 2XA    Name  AW EPR
50211   Mrs Godwin  64 Riverview    MK43 7PN       Name  FPA - 28

目的の出力を取得するための同じことに対する別のクエリ

select *
from
(
   select OrderID, AccessName, Address1, Postcode, GeneralInstructions, --CompanyName,
    col, --col+'_'+cast(rn as varchar(10)) col, 
    val
  from 
  (
    select o.OrderID, js.JobStatusID, p.Name, o.AccessName, o.Address1, o.Postcode, oj.GeneralInstructions, s.CompanyName
     , row_number() over(partition by o.OrderID,js.JobStatusID 
                      order by o.OrderID) rn
   FROM 
   NEPCCO.Orders o
   inner join NEPCCO.Clients c on o.ClientID = c.ClientID
   inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
   inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
   inner join NEPCCO.Products p on oj.ProductID = p.ProductID
   inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
   inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
   where 
   o.OrderID in (50211, 44621) and 
   (p.ProductID in (35,36,37,38,38,40,41,42,43) or  p.ProductID in (33,34))
    ) d
   unpivot
  (
   val
   for col in (Name)
  ) un
  ) s
 pivot
 (
 max(val)
 for col in (Name,JobStatusID,  JobStatusID1, Name1)
) piv

望ましいものに近いが、私が望むものではない2番目の出力

OrderID     AccessName      Address1            PostCode   Col              Val 
 44621  Mr Hayden   7 Broad Rush Green  LU7 2XA  FPA - 28   NULL     
 50211  Mrs Godwin  64 Riverview        MK43 7PN     FPA - 28   NULL    

望ましい出力 - 最初のクエリから、表示したい列ヘッダー名を確認します

OrderID     AccessName      Address1           PostCode   InstallJobType  EPR Type   
 44621  Mr Hayden   7 Broad Rush Green LU7 2XA    FPA - 28        AW EPR
 50211  Mrs Godwin  64 Riverview    MK43 7PN      FPA - 28        NULL  

私が立ち往生しているので、どんな助けもいただければ幸いです


アンピボット前のデータ

OrderID     JobstatusId   Name      AccessName        Address1             PostCode    CompanyName         M

   44621    3         AW EPR    Mr Hayden   7 Broad Rush Green  LU7 2XA   N/A (Sole trader)                 1
   44621    14        FPA - 28  Mr Hayden   7 Broad Rush Green  LU7 2XA Just Energy Solutions Limited       2
   50211    17        FPA - 28  Mrs Godwin  64 Riverview        MK43 7PN    Just Energy Solutions Limited   1

編集 - 希望の出力が得られるので、正しく実行しているかどうかを確認してください

select OrderID, AccessName, Address1, Postcode, GeneralInstructions, 
Name_1 as "Install Jobs", 
Name_2 as "EPR Jobs",
CompanyName,Name
from
(
  select OrderID, AccessName, Address1, Postcode, GeneralInstructions, CompanyName,
    col+'_'+cast(rn as varchar(10)) col, 
    val
  from 
  (
    select o.OrderID, js.JobStatusID, p.Name, o.AccessName, o.Address1, o.Postcode, oj.GeneralInstructions, s.CompanyName, 
    js.Name as "Install Job Status", js.JobStatusID as JS1
      , row_number() over(partition by o.OrderID 
                          order by o.OrderID) rn
    FROM 
    NEPCCO.Orders o
    inner join NEPCCO.Clients c on o.ClientID = c.ClientID
    inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
    inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
    inner join NEPCCO.Products p on oj.ProductID = p.ProductID
    inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
    inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
    where 
    o.OrderID in (50211, 44621) and 
    (p.ProductID in (35,36,37,38,38,40,41,42,43) or  p.ProductID in (33,34))
  ) d
  unpivot
  (
    val
    for col in (Name)
  ) un
) s
pivot
(
  max(val)
  for col in (JobStatusID_1, Name_1,  JobStatusID_2, Name_2,Name)
) piv
4

1 に答える 1

3

UNPIVOT をName列のみに適用しているため、現在のクエリは混乱を招きます。UNPIVOT は、複数の列を複数の行に変換するために使用されます。nameクエリのPIVOT部分にある列に基づいて、列と列をUNPIVOTする必要があると推測していJobStatusます。

それが正しければ、クエリは次のようになります。

select *
from
(
   select OrderID, AccessName, Address1, Postcode, 
    GeneralInstructions, --CompanyName,
    col+cast(rn as varchar(10)) col,
    val
  from 
  (
    select o.OrderID, 
      cast(js.JobStatusID as varchar(50)) JobStatusId, 
      cast(p.Name as varchar(50)) Name, 
      o.AccessName, o.Address1, o.Postcode, 
      oj.GeneralInstructions, s.CompanyName
     , row_number() over(partition by o.OrderID,js.JobStatusID 
                      order by o.OrderID) rn
     FROM NEPCCO.Orders o
     inner join NEPCCO.Clients c on o.ClientID = c.ClientID
     inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
     inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
     inner join NEPCCO.Products p on oj.ProductID = p.ProductID
     inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
     inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
     where o.OrderID in (50211, 44621) and 
       (p.ProductID in (35,36,37,38,38,40,41,42,43) or  p.ProductID in (33,34))
  ) d
  unpivot
  (
    val
    for col in (Name, JobStatus)
  ) un
) s
pivot
(
  max(val)
  for col in (Name, JobStatusID,  JobStatusID1, Name1)
) piv;

編集、値の数が不明な場合は、動的 SQL を使用する必要があります。

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT  ',' + QUOTENAME(col+cast(seq as varchar(10))) 
                    from
                    (
                       select   row_number() over(partition by o.OrderID,js.JobStatusID 
                          order by o.OrderID) seq
                       FROM NEPCCO.Orders o
                       inner join NEPCCO.Clients c on o.ClientID = c.ClientID
                       inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
                       inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
                       inner join NEPCCO.Products p on oj.ProductID = p.ProductID
                       inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
                       inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
                    ) d
                    cross apply
                    (
                      select 'Name', 1 union all
                      select 'JobStatusId', 2
                    ) c (col, so)
                    group by seq, col, so
                    order by seq, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT *
            from 
            (
               select OrderID, AccessName, Address1, Postcode, 
                GeneralInstructions, --CompanyName,
                col+cast(rn as varchar(10)) col,
                val
              from 
              (
                select o.OrderID, 
                  cast(js.JobStatusID as varchar(50)) JobStatusId, 
                  cast(p.Name as varchar(50)) Name, 
                  o.AccessName, o.Address1, o.Postcode, 
                  oj.GeneralInstructions, s.CompanyName
                 , row_number() over(partition by o.OrderID,js.JobStatusID 
                                  order by o.OrderID) rn
                 FROM NEPCCO.Orders o
                 inner join NEPCCO.Clients c on o.ClientID = c.ClientID
                 inner join NEPCCO.OrderJobs oj on o.OrderID = oj.OrderID
                 inner join NEPCCO.Suppliers s on oj.SupplierID = s.SupplierID
                 inner join NEPCCO.Products p on oj.ProductID = p.ProductID
                 inner join NEPCCO.OrderStatus os on o.OrderStatusID = os.OrderStatusID
                 inner join NEPCCO.JobStatus js on oj.JobStatusID = js.JobStatusID
                 where o.OrderID in (50211, 44621) and 
                   (p.ProductID in (35,36,37,38,38,40,41,42,43) or  p.ProductID in (33,34))
              ) d
              unpivot
              (
                val
                for col in (Name, JobStatus)
              ) un
            ) x
            pivot 
            (
                max(val)
                for col in (' + @cols + ')
            ) p '

execute(@query)
于 2013-06-26T12:50:49.817 に答える