一列上的组,基于另一列中不同值的汇总计数

发布于 2025-02-12 11:14:35 字数 994 浏览 0 评论 0原文

我有一个具有类似值的表:

CREATE TABLE myTable
    (`ts` varchar(2), `usr_id` varchar(1), `data` varchar(1))
;
    
INSERT INTO myTable
    (`ts`, `usr_id`, `data`)
VALUES
    ('11', 'A', 'x'),
    ('11', 'A', 'x'),
    ('11', 'B', 'x'),
    ('11', 'C', 'x'),
    ('11', 'C', 'x'),
    ('11', 'C', 'x'),
    ('22', 'B', 'x'),
    ('33', 'C', 'x'),
    ('33', 'C', 'x'),
    ('33', 'D', 'x'),
    ('33', 'A', 'x')  
;

我想根据TS进行组,并显示以下usr_id的不同值的计数。

t的TS计数11计数
21322
01033
103<

a href =“ http://www.sqlfiddle.com/#!9/bbbf4fc/2” “> http://www.sqlfiddle.com/#!9/bbf4fc/2

我无法取得太大的进步通过TS进行组:从Mytable组中选择TS,USR_ID,count(*)由TS,usr_id; 但是我希望在同一行中使用A,B和其他人的行计数,无法弄清楚如何实现这一目标。

I have a table with values like so:

CREATE TABLE myTable
    (`ts` varchar(2), `usr_id` varchar(1), `data` varchar(1))
;
    
INSERT INTO myTable
    (`ts`, `usr_id`, `data`)
VALUES
    ('11', 'A', 'x'),
    ('11', 'A', 'x'),
    ('11', 'B', 'x'),
    ('11', 'C', 'x'),
    ('11', 'C', 'x'),
    ('11', 'C', 'x'),
    ('22', 'B', 'x'),
    ('33', 'C', 'x'),
    ('33', 'C', 'x'),
    ('33', 'D', 'x'),
    ('33', 'A', 'x')  
;

I want to do a GROUP BY based on ts and display the counts for distinct values of usr_id like below.

tscount of Acount of Bcount of others
11213
22010
33103

http://www.sqlfiddle.com/#!9/bbf4fc/2

I couldn't make much progress beyond doing the GROUP BY for ts: select ts, usr_id, count(*) from myTable group by ts, usr_id;
But I want the counts of rows with A, B and others in the same row, can't figure out how to achieve that.

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

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

发布评论

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

评论(3

孤千羽 2025-02-19 11:14:35

这是您的查询

SELECT 
    DISTINCT ts, 
    COUNT(CASE WHEN usr_id = 'A' THEN 1 END) AS count_a,
    COUNT(CASE WHEN usr_id = 'B' THEN 1 END) AS count_b,
    COUNT(CASE WHEN usr_id != 'A' AND usr_id != 'B' THEN 1 END) AS count_others
FROM myTable
GROUP BY ts
ORDER BY ts

This is your query

SELECT 
    DISTINCT ts, 
    COUNT(CASE WHEN usr_id = 'A' THEN 1 END) AS count_a,
    COUNT(CASE WHEN usr_id = 'B' THEN 1 END) AS count_b,
    COUNT(CASE WHEN usr_id != 'A' AND usr_id != 'B' THEN 1 END) AS count_others
FROM myTable
GROUP BY ts
ORDER BY ts

Demo

冷默言语 2025-02-19 11:14:35

PostgreSQL允许过滤聚合:

select ts, 
       count(1) filter (where usr_id = 'A') as "count of A",
       count(1) filter (where usr_id = 'B') as "count of B",
       count(1) filter (where usr_id not in ('A', 'B')) as "count of others"
  from mytable
 group by ts;

db&lt;&gt; fiddle 此处

PostgreSQL allows filtering aggregates:

select ts, 
       count(1) filter (where usr_id = 'A') as "count of A",
       count(1) filter (where usr_id = 'B') as "count of B",
       count(1) filter (where usr_id not in ('A', 'B')) as "count of others"
  from mytable
 group by ts;

db<>fiddle here

你在看孤独的风景 2025-02-19 11:14:35

您的示例链接是ybout mysql,但是

SELECT
  ts
  , SUM(`usr_id` = 'A') CountA
  , SUM(`usr_id` = 'B') CountB
  , SUM(`usr_id` <> 'B' AND `usr_id` = 'B') CountElse
  
from myTable group by ts

sample

和posdgres

select 
  ts
  , SUM(CASE WHEN "usr_id" = 'A' THEN 1 ELSE 0 END) CountA
  , SUM(CASE WHEN "usr_id" = 'B' THEN 1 ELSE 0 END) CountB
  , SUM(CASE WHEN "usr_id" <> 'B' AND "usr_id" = 'B' THEN 1 ELSE 0 END) CountElse
  
  from myTable group by ts

请参见示例

your sample link is ybout mysql, but

SELECT
  ts
  , SUM(`usr_id` = 'A') CountA
  , SUM(`usr_id` = 'B') CountB
  , SUM(`usr_id` <> 'B' AND `usr_id` = 'B') CountElse
  
from myTable group by ts

Sample

And for posdgres

select 
  ts
  , SUM(CASE WHEN "usr_id" = 'A' THEN 1 ELSE 0 END) CountA
  , SUM(CASE WHEN "usr_id" = 'B' THEN 1 ELSE 0 END) CountB
  , SUM(CASE WHEN "usr_id" <> 'B' AND "usr_id" = 'B' THEN 1 ELSE 0 END) CountElse
  
  from myTable group by ts

see example

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