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_ID 和 ACC_AMT 在不同的列中。该表可能还有其他不同的 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

使用pivot子句它可以帮助你,链接

于 2013-05-20T12:32:59.707 回答
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

多谢你们。但我得到了答案: 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 回答