以下のようなクエリがあり、次の結果が得られます。
クエリ
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