Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

sql - Oracle Rows to Column Transformation

I have a table

select * from table

values returning are

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

how can i transfer this data from row to column? I want in this manner

login_id | open | closed | inprogress | pending
================================================
admin    |    3 |      5 |          0 |       0
test     |   10 |     10 |         10 |       0
user1    |    0 |      5 |          0 |      10
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...