如何换位

时间:2019-05-13 22:58:02

标签: sql oracle

DB:Oracle

我有几个字段,

account cost1 cost2 cost3 cost4 cost5
10       $20   $30   $40   $50   $60
20       $100   $200  $300  $400 $500

要求是

  • 如果帐户是10,那么我只需要cost1和cost3

  • 如果帐户是20,那么我只需要cost2和cost4

结果应该是这样的,

20----10
40----10
200----20
400----20

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

使用union all

select account, cost1 as cost
from t
where account = 10
union all
select account, cost3
from t
where account = 10
union all
select account, cost2
from t
where account = 20
union all
select account, cost4
from t
where account = 20