Vim*_*987 1 sql database oracle plsql
我有这样一张桌子:
UserID     Customer ID status        
1               1          1
1               2          1 
1               3          1
1               4          2
1               5          1
1               6          3
1               7          2
2               8          1
2               9          2 
 ........
我想总结一下这个表,对此:
 UserID           count(status 1)    count(status 2)   count(status 3)
    1                4                2                     1 
    2                1                2                     3
   .........
我怎么能在PL/SQL中做到这一点?
预先感谢
Guf*_*ffa 10
您可以对UserId进行分组并总结不同的状态代码.
就像是:
select
  UserId,
  sum(case status when 1 then 1 else 0 end) as Status1,
  sum(case status when 2 then 1 else 0 end) as Status2,
  sum(case status when 3 then 1 else 0 end) as Status3
from SomeTable
group by UserId
order by UserId
您也可以考虑简单地对UserId和状态进行分组,尽管结果当然不同:
select UserId, status, count(*)
from SomeTable
group by UserId, status
order by UserId, status
| 归档时间: | 
 | 
| 查看次数: | 3395 次 | 
| 最近记录: |