2017-05-28 16 views
-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

私は必要なものである各レベルでロールアップする

  1. セグメント
  2. リージョン
  3. クラスタ
  4. エリア
  5. 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

希望として働いているが、それらは各詳細レベルの最上部に表示され、代わりに下の。

ボトムアップを行うときに助けが必要です。

答えて

0

基本出力クエリでgroup by句をgroup by grouping setsに変更するだけです。

Grouping Sets Documentation

NULLの値を空白にする必要があるかもしれませんが、計算結果は各レベルで一致します。

回答:

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 grouping sets 
    (
     (TS.TargetDesc, Area, Cluster, Region, Segment, TS.Cycle) --lowest level 
     , (Area, Cluster, Region, Segment, TS.Cycle) 
     , (Cluster, Region, Segment, TS.Cycle) 
     , (Region, Segment, TS.Cycle) 
     , (Segment, TS.Cycle) 
    ) 
関連する問題