Sql 如何转置

Sql 如何转置,sql,oracle,Sql,Oracle,DB:Oracle 我有几个领域 account cost1 cost2 cost3 cost4 cost5 10 $20 $30 $40 $50 $60 20 $100 $200 $300 $400 $500 要求是, 如果帐户是10,那么我只需要成本1和成本3 如果帐户是20,那么我只需要成本2和成本4 结果应该是这样的, 20----10 40----10 200----20 400----20 有什么建议吗?使用union all:

DB:Oracle

我有几个领域

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

  • 如果帐户是10,那么我只需要成本1和成本3

  • 如果帐户是20,那么我只需要成本2和成本4

结果应该是这样的,

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

有什么建议吗?

使用
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

使用“全部联合”:

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