如何统计表中某一列的每个值?

发布于 2024-09-24 11:36:54 字数 650 浏览 3 评论 0原文

我有一个这样的表:

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 中做到这一点?

提前致谢

I have a table like this:

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 
 ........

I want to summarize this table, to this:

 UserID           count(status 1)    count(status 2)   count(status 3)
    1                4                2                     1 
    2                1                2                     3
   .........

How can I do that in PL/SQL?

Thank in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情魔剑神 2024-10-01 11:36:54

您可以根据 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

You can group on UserId and sum up the different status codes.

Something like:

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

You might also consider simply grouping on UserId and status, although the result is of course differently laid out:

select UserId, status, count(*)
from SomeTable
group by UserId, status
order by UserId, status
荆棘i 2024-10-01 11:36:54
select userid, 
       count(decode(status, 1, 1, null)),
       count(decode(status, 2, 1, null)),
       count(decode(status, 3, 1, null)),
  from table
 group by userid
select userid, 
       count(decode(status, 1, 1, null)),
       count(decode(status, 2, 1, null)),
       count(decode(status, 3, 1, null)),
  from table
 group by userid
一抹微笑 2024-10-01 11:36:54
SELECT *
  FROM ( SELECT UserID,
                status,
                COUNT(status)
           FROM <table>
          GROUP BY UserID,
                   status
       )
 PIVOT(COUNT(status) FOR status IN (1,2,3))
SELECT *
  FROM ( SELECT UserID,
                status,
                COUNT(status)
           FROM <table>
          GROUP BY UserID,
                   status
       )
 PIVOT(COUNT(status) FOR status IN (1,2,3))
哀由 2024-10-01 11:36:54

只是为了跟进@Vimvq1987和@Guffa评论:SQL的正确语法是case ... end,但对于PL/SQL它应该是 case ... end case,因此您提供的链接上的信息是正确的。

因此,在 SQL 查询中(无论是在 SQL-Plus 中执行还是在 PL/SQL 中的 DML 中执行),您应该使用 case ... end,但在 PL/SQL 例程中使用 case 。 .. end case 是必需的。

Just to follow up @Vimvq1987 and @Guffa comments: the correct syntax for SQL is case ... end, but for PL/SQL it sould be case ... end case, so the information on the link you've provided is right.

Hence in your SQL queries (either you execute it in SQL-Plus or in the DML in PL/SQL) you should use case ... end, but in PL/SQL routines case ... end case is required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文