小编典典

Oracle行到列的转换

sql

我有桌子

select * from table

返回的值是

login id | status_name | count
===============================
admin    | open        |     3
admin    | closed      |     5
test     | inprogress  |    10
test     | open        |    10
test     | closed      |    11
user1    | closed      |     5
user1    | pending     |    10

如何将这些数据从行转移到列?我要这样

login_id | open | closed | inprogress | pending
================================================
admin    |    3 |      5 |          0 |       0
test     |   10 |     10 |         10 |       0
user1    |    0 |      5 |          0 |      10

阅读 148

收藏
2021-05-05

共1个答案

小编典典

select login_id
     , sum(case when status_name='open' then count end) open
     , sum(case when status_name='closed' then count end) closed
     , sum(case when status_name='inprogress' then count end) inprogress
     , sum(case when status_name='pending' then count end) pending
from table
group by login_id
2021-05-05