ドロップHoursとDollarsを「2列セット」として出力しようとしていて、率直に言って、ピボットのT-SQL実装があまりにも制限されていることがわかりました。アンピボットとピボットを併用して、あなたが望む効果を得る方法を試みることができます(example here)。
個人的には、必要に応じてGROUP BY
とcase expressions
という単純なピボットクエリを使用していました(「ピボットがSQLにすべて追加された」というスタイルです)あなた次第エイリアス)テストされていない
SELECT
[Facility]
, [Job_Code]
, SUM(CASE WHEN Pay_ClassHrs = 'PBS_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'POT_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'PED_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'POR_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'Agency_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'PWH_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'Sick_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'PTO_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'Holiday_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'PJB_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'Bugeted_Work_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_ClassHrs = 'Bugeted_Benefit_Hrs' THEN Pay_ClassHrs END) AS x
, SUM(CASE WHEN Pay_Class$ = 'POT_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'PED_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'POR_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'Agency_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'PWH_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'Sick_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'PTO_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'Holiday_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'PJB_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'PNH_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'Bugeted_Work_$' THEN Pay_Class$ END) AS x
, SUM(CASE WHEN Pay_Class$ = 'Bugeted_Benefit_$' THEN Pay_Class$ END) AS x
FROM [#PremierThree]
GROUP BY
[Facility]
, [Job_Code]
!
上記のアプローチは、あなたのためにあまりにも古い帽子と長ったらしいに見える場合。参加を使用して2つのピボットを組み合わせて試してみてください、私はHAVの下両方のサブクエリが同じ行()を生成する場合、より簡単なinner join
で動作する可能性があります。その場合は、そのテンポラリテーブルを使用している可能性があります。
SELECT
COALESCE(p1.Facility,p2.Facility) Facility
, COALESCE(p1.Job_Code,p2.Job_Code) Job_Code
, [PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs], [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs]
, [PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$]
FROM (
SELECT
[Facility]
, [Job_Code]
, [PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs], [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs]
FROM [#PremierThree]
PIVOT
(SUM(PayHours)
FOR [Pay_ClassHrs] IN ([PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs], [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs])
) AS pvt1
) AS p1
FULL OUTER JOIN (
SELECT
[Facility]
, [Job_Code]
, [PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$]
PIVOT
(SUM(PayAmount)
FOR [Pay_Class$] in ([PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$])
) AS pvt2
) AS p2
ON p1.Facility = p2.Facility AND p1.Job_Code = p2.Job_Code
ORDER BY
[Facility], CAST([Job_Code] AS INT)
;
T-SQLの場合:個人的には、結合アプローチを使用しても、複雑になるときにピボットを使用する利点はありません。
あなたの両親はかなり明らかに不均衡です。 ))pvt1 ...なぜ2つの閉じた括弧は1つの開いている括弧のためだけですか? – pmbAustin
は私のために正常にコンパイルされます – Artem
これらの大括弧がすべて必要なわけではありません。 –