Oracle 行到列的转换
Posted
技术标签:
【中文标题】Oracle 行到列的转换【英文标题】:Oracle Rows to Column Transformation 【发布时间】:2012-03-06 10:53:53 【问题描述】:我有一张桌子
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
【问题讨论】:
也可以尝试查看 PIVOT:例如 ***.com/questions/4841718/oracle-sql-pivot-query 【参考方案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
【讨论】:
嗨...感谢您提供查询,但计数未显示...还有什么方法可以写动态以上是关于Oracle 行到列的转换的主要内容,如果未能解决你的问题,请参考以下文章
如何在 sql server 2008 中编写对行到列的查询?