0

IBM DB2 SQL を使用しています。

これが私の現在のクエリです:

select 
EXSHPE as "Shape",
EXDLVY as "Delivery",

  Sum(Case When EXSURF = 'Print' Then EXRLTO Else 0 End) As Retail_Print,
  Sum(Case When EXSURF = 'Pattern' Then EXRLTO Else 0 End) as Retail_Pattern,
  Sum(Case When EXSURF = 'Solid' Then EXRLTO Else 0 End) As Retail_Solid,
  Sum(Case When EXSURF = 'UnknownA' Then EXRLTO Else 0 End) as Retail_UnknownA,
  Sum(Case When EXSURF = 'UnknownB' Then EXRLTO Else 0 End) As Retail_UnknownB,
  Sum(Case When EXSURF = 'UnknownC' Then EXRLTO Else 0 End) as Retail_UnknownC,

  Sum(Case When EXSURF = 'Print' Then EXWHLO Else 0 End) As Wholesale_Print,
  Sum(Case When EXSURF = 'Pattern' Then EXWHLO Else 0 End) as Wholesale_Pattern,
  Sum(Case When EXSURF = 'Solid' Then EXWHLO Else 0 End) As Wholesale_Solid,
  Sum(Case When EXSURF = 'UnknownA' Then EXWHLO Else 0 End) as Wholesale_UnknownA,
  Sum(Case When EXSURF = 'UnknownB' Then EXWHLO Else 0 End) As Wholesale_UnknownB,
  Sum(Case When EXSURF = 'UnknownC' Then EXWHLO Else 0 End) as Wholesale_UnknownC,

  Sum(Case When EXSURF = 'Print' Then EXUNTO Else 0 End) As Units_Print,
  Sum(Case When EXSURF = 'Pattern' Then EXUNTO Else 0 End) as Units_Pattern,
  Sum(Case When EXSURF = 'Solid' Then EXUNTO Else 0 End) As Units_Solid,
  Sum(Case When EXSURF = 'UnknownA' Then EXUNTO Else 0 End) as Units_UnknownA,
  Sum(Case When EXSURF = 'UnknownB' Then EXUNTO Else 0 End) As Units_UnknownB,
  Sum(Case When EXSURF = 'UnknownC' Then EXUNTO Else 0 End) as Units_UnknownC,

  Sum(Case When EXSURF = 'Print' Then EXAURA Else 0 End) As Actual_AUR_Print,
  Sum(Case When EXSURF = 'Pattern' Then EXAURA Else 0 End) as Actual_AUR_Pattern,
  Sum(Case When EXSURF = 'Solid' Then EXAURA Else 0 End) As Actual_AUR_Solid,
  Sum(Case When EXSURF = 'UnknownA' Then EXAURA Else 0 End) as Actual_AUR_UnknownA,
  Sum(Case When EXSURF = 'UnknownB' Then EXAURA Else 0 End) As Actual_AUR_UnknownB,
  Sum(Case When EXSURF = 'UnknownC' Then EXAURA Else 0 End) as Actual_AUR_UnknownC,

  Sum(Case When EXSURF = 'Print' Then EXMERA Else 0 End) As Merch_AUR_Print,
  Sum(Case When EXSURF = 'Pattern' Then EXMERA Else 0 End) as Merch_AUR_Pattern,
  Sum(Case When EXSURF = 'Solid' Then EXMERA Else 0 End) As Merch_AUR_Solid,
  Sum(Case When EXSURF = 'UnknownA' Then EXMERA Else 0 End) as Merch_AUR_UnknownA,
  Sum(Case When EXSURF = 'UnknownB' Then EXMERA Else 0 End) As Merch_AUR_UnknownB,
  Sum(Case When EXSURF = 'UnknownC' Then EXMERA Else 0 End) as Merch_AUR_UnknownC

from EXOWMSPD
Where (EXCO || '/' || EXDIV)  = ?
Group By
  EXSHPE,
  EXDLVY
Order By
  EXSHPE DESC,
  EXDLVY DESC

データベースは次のようになります。 ここに画像の説明を入力

Surface をピボットする必要がありますが、動的です。問題は、最初の 3 つのサーフェスの説明しか知らないことです。ただし、最大 6 の準備が必要です。

これを動的にピボットして最初の 6 つのサーフェスを取得する方法はありますか?

例、

RETAIL_DOLLARS_1   10.00
RETAIL_DOLLARS_2   20.00
RETAIL_DOLLARS_3   50.00
RETAIL_DOLLARS_4   0.00
RETAIL_DOLLARS_5   0.00
RETAIL_DOLLARS_6   0.00

この例では、3 つのサーフェスのデータが見つかり、残りの 3 つは 0 で埋めたいと思います。

これを使用してレポートを作成し、SURFACE_DESCRIPTION_X が "" に等しい列を非表示にします。

これを行う方法はありますか?

ありがとう

4

1 に答える 1

0

さまざまな次元でデータをキューブ化する場合は、

 GROUP BY CUBE

于 2012-11-07T14:27:54.013 に答える