2012-02-17 16 views
2

ここで問題が発生した最初のクエリ:Tricky GROUP BY issue on ORACLEが確実に解決されました。ORACLEのPIVOT/GROUP BY問題

しかし、私は新しい質問があります。私はそれをもう一度変換しようとします:

 
         |   EMAIL   |    WIFI   | ...   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
     Yes    |    20   |    24   | ...     
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
     No    |    4   |     0   | ...   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    Unknown    |    1   |     1   | ... 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

ここでそのような出力を構築するのに役立つデータです。私はRenéが私が引用した解決済みの問題で私に与えた問合せで再びunpivot/pivotを使用しようとしましたが、残念ながら "ORA-56901:ピボット| unpivot値ではない式が使用できません"というエラーが出ます。 ..

 
with 
count_table as (
    select 1001 device_id, 4 quantity from dual union all 
    select 1002 device_id, 20 quantity from dual union all 
    select 1003 device_id, 1 quantity from dual 
), 
device_table as (
    select 1001 id, 'Yes'  wifi, 'No'  email, 'No' bluetooth from dual union all 
    select 1002 id, 'Yes'  wifi, 'Yes'  email, 'No' bluetooth from dual union all 
    select 1003 id, 'Unknown' wifi, 'Unknown' email, 'Yes' bluetooth from dual 
) 

おそらくもっと簡単な解決法がありますか?リレーショナルDBについての本を読んでおく必要があります:)

答えて

1

.. そのため、以下のクエリを試してみてください...

with 
count_table as (
    select 1001 device_id, 4 quantity from dual union all 
    select 1002 device_id, 20 quantity from dual union all 
    select 1003 device_id, 1 quantity from dual 
), 
device_table as (
    select 1001 id, 'Yes'  wifi, 'No'  email, 'No' bluetooth from dual union all 
    select 1002 id, 'Yes'  wifi, 'Yes'  email, 'No' bluetooth from dual union all 
    select 1003 id, 'Unknown' wifi, 'Unknown' email, 'Yes' bluetooth from dual 
) 
---------------------------------------- 
select * from (
     select 
     feature, 
     yes_no_unknown, 
     sum(quantity) quantity 
     from 
     count_table c join 
     device_table d on c.device_id = d.id 
     unpivot (yes_no_unknown 
       for feature in (wifi, email, bluetooth) 
    ) 
     group by 
     feature, 
     yes_no_unknown 
) 
pivot (sum (quantity) 
     -- only this line I have changed .. 
     for feature in ('WIFI' as Wifi, 'EMAIL' as Email, 'BLUETOOTH' as Bluetooth) 
); 
+0

ありがとうございました!それは機能します(btw、機能語の入力ミスがあります) 私はこのPIVOT/UNPIVOT機能をよりよく理解し始めます;) – Ajantis

0

出力表の列の数に柔軟性がある場合は、おそらくいくつかの手続き型ソリューションを使用できます。 PL/SQLまたはJava。

PL/SQLでは、2次元コレクションを作成し、それを移入して印刷することができます。あなたはdbms_sqlパッケージを使用して動的SQLクエリを作成/生成できます。

それはあなたの前のポストを参照した後、非常に単純な探してい
関連する問題