如何构造SQL查询以从不同列中的同一个表中获取不同的行?

时间:2013-05-20 12:14:23

标签: sql oracle row fetch

假设我在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>

但我没有得到理想的结果。

3 个答案:

答案 0 :(得分:0)

使用pivot子句可能会对您有所帮助,Link

答案 1 :(得分:0)

Comment and situation

您可以尝试使用以下查询 -

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

这应该可以根据需要为您提供输出。

答案 2 :(得分: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
  /