-2
現在、地域>エリア>クラスター>地域>セグメントに基づいて販売データを保持するテーブルがあります。私が必要とするのは、基本的にボトルトンの各レベルでロールアップを表示できる出力を得ることです。TSQLでExcel類似ピボットを作成する
問い合わせの上select
TS.Cycle,
Segment,
Region,
Cluster,
Area,
TS.TargetDesc,
Sum(TS.Target) as TGT,
sum(A.Achivement) as ACH,
0 as [lvl]
from MST_TargetSetup TS
join MST_AchivementSetup A on
TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and
A.AchivementType='T' and
(cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by TS.TargetDesc, Area, Cluster, Region, Segment, TS.Cycle
私に次の出力を与える:出力として今base output
私は必要なものである各レベルでロールアップする
- セグメント
- リージョン
- クラスタ
- エリア
- TargetDesc
以下の異なるグループの結合を使用しようとしましたが、各レベルの下部の代わりに上部にロールアップを作成しました。
select
TS.Cycle,Segment,Region,Cluster,Area,TS.TargetDesc, Sum(TS.Target) as TGT,sum(A.Achivement) as ACH from MST_TargetSetup TS
join MST_AchivementSetup A on TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and A.AchivementType='T' and (cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by TS.TargetDesc,Area,Cluster,Region,Segment,TS.Cycle
union
select
TS.Cycle,Segment,'' as Region,'' as Cluster,'' as Area,'' as TargetDesc, Sum(TS.Target) as TGT,sum(A.Achivement) as ACH from MST_TargetSetup TS
join MST_AchivementSetup A on TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and A.AchivementType='T' and (cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by Segment,TS.Cycle
union
select
TS.Cycle,Segment,Region,'' as Cluster,'' as Area,'' as TargetDesc, Sum(TS.Target) as TGT,sum(A.Achivement) as ACH from MST_TargetSetup TS
join MST_AchivementSetup A on TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and A.AchivementType='T' and (cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by Segment,Region,TS.Cycle
union
select
TS.Cycle,Segment,Region,cluster,'' as Area,'' as TargetDesc, Sum(TS.Target) as TGT,sum(A.Achivement) as ACH from MST_TargetSetup TS
join MST_AchivementSetup A on TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and A.AchivementType='T' and (cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by Segment,Region,cluster,TS.Cycle
union
select
TS.Cycle,Segment,Region,Cluster,Area,'' as TargetDesc, Sum(TS.Target) as TGT,sum(A.Achivement) as ACH from MST_TargetSetup TS
join MST_AchivementSetup A on TS.TargetDesc = A.AchivementDesc and Ts.TargetType='T' and A.AchivementType='T' and (cast(Ts.Month as varchar(5))+'-'+cast(Ts.Year as varchar(5)))=(cast(A.Month as varchar(5))+'-'+cast(A.Year as varchar(5)))
join MST_EmployeeMaster E on TS.TargetDesc=E.Terriory
Group by Segment,Region,Cluster,Area,TS.Cycle
出力:出力ロールアップの上に表示された場合はoutput with union
希望として働いているが、それらは各詳細レベルの最上部に表示され、代わりに下の。
ボトムアップを行うときに助けが必要です。