0

ORACLE データベースに次のようなテーブルがあるとします。

ACC_ID | ACC_AMT
111    | 10000
111    | 12000
111    | 14000
222    | 25000
222    | 30000
333    | 18000
333    | 27000
333    | 13000
333    | 15000

出力を次のように取得したい:

ACC_ID_1 | ACC_AMT_1 | ACC_ID_2 | ACC_AMT_2 | ACC_ID_3 | ACC_AMT_3
111      | 10000     | 222      | 25000     | 333      | 18000
111      | 12000     | 222      | 30000     | 333      | 27000
111      | 14000     | null     | null      | 333      | 13000
null     | null      | null     | null      | 333      | 15000

異なる列に ACC_AMT を持つそれぞれ異なる ACC_ID が必要です。テーブルには他にも異なる ACC_ID がある場合がありますが、必要なものだけをフェッチします。これを行う最善の方法は何ですか?

これまでのところ、私はこれを試しました:

SELECT 
(CASE WHEN ACC_ID=111 THEN ACC_ID END) AS ACC_ID_1,
(CASE WHEN ACC_ID=111 THEN ACC_AMT END) AS ACC_AMT_1,
(CASE WHEN ACC_ID=222 THEN ACC_ID END) AS ACC_ID_2,
(CASE WHEN ACC_ID=222 THEN ACC_AMT END) AS ACC_AMT_2,
(CASE WHEN ACC_ID=333 THEN ACC_ID END) AS ACC_ID_3,
(CASE WHEN ACC_ID=333 THEN ACC_AMT END) AS ACC_AMT_3
FROM <TABLE_NAME>

しかし、私は望ましい結果を得ていません。

4

3 に答える 3

0

みんなありがとう。しかし、次の回答を得ました: http://www.orafaq.com/forum/t/187775/178634/

with 
    data as  (
      select acc_id, acc_amt,
             dense_rank() over(order by acc_id) rk,
             row_number() over(partition by acc_id order by acc_amt) rn
      from t
    )
  select max(decode(rk, 1, acc_id))  acc_id_1,
         max(decode(rk, 1, acc_amt)) acc_amt_1,
         max(decode(rk, 2, acc_id))  acc_id_2,
         max(decode(rk, 2, acc_amt)) acc_amt_2,
         max(decode(rk, 3, acc_id))  acc_id_3,
         max(decode(rk, 3, acc_amt)) acc_amt_3
  from data
  group by rn
  order by rn
  /
于 2013-05-29T12:11:54.287 に答える
0

コメントと状況

次のクエリで試すことができます-

select table1.acc_id acc_id_1,table1.acc_amt acc_amt_1,
       table2.acc_id acc_id_2,table2.acc_amt acc_amt_2,       
       table3.acc_id acc_id_3,table3.acc_amt acc_amt_3,
       table4.acc_id acc_id_4,table4.acc_amt acc_amt_4,
       table5.acc_id acc_id_5,table5.acc_amt acc_amt_5,
       table6.acc_id acc_id_6,table6.acc_amt acc_amt_6,
       table7.acc_id acc_id_7,table7.acc_amt acc_amt_7,
       table8.acc_id acc_id_8,table8.acc_amt acc_amt_8,
       table9.acc_id acc_id_9,table9.acc_amt acc_amt_9,
       table10.acc_id acc_id_10,table10.acc_amt acc_amt_10
 from 
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '111') table1 
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '222') table2 
on table2.rn = table1.rn
full outer join 
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '333') table3 
on table3.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '444') table4 
on table4.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '555') table5 
on table5.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '666') table6 
on table6.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '777') table7 
on table7.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '888') table8 
on table8.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '999') table9 
on table9.rn = table1.rn
full outer join
(select acc_id, acc_amt, rownum rn from <Table_name> where acc_id = '101') table10 
on table10.rn = table1.rn

これにより、必要な出力が得られるはずです。

于 2013-05-20T14:12:39.710 に答える
0

ピボット句を使用すると役立つ場合があります。リンク

于 2013-05-20T12:32:59.707 に答える