如果包含在两个组中,则按分组计数并分开

Count Group By and Separate If Included in Both Group

不确定这个问题是否重复。

下面我有一个简化的table

User Interest
Jason Art
Jason Sport
Sam Sport
Sam Art
Steve Sport
Desmond Sport
Tania Art

这是我想要达到的结果

Interest Count
Art 2
Sport 2
Both 2

我设法通过此查询

创建了一个子查询来实现 Both 数据的值
SELECT COUNT(USER) FROM (
SELECT User, COUNT(DISTINCT Interest) as interest_type FROM table WHERE interest_type = 2)

但是对于在ArtSport中独占Interest的用户来说是不分开的。

您可以在此处使用条件聚合:

WITH cte AS (
    SELECT User,
           CASE WHEN COUNT(CASE WHEN Interest = 'Art' THEN 1 END) > 0 AND
                     COUNT(CASE WHEN Interest = 'Sport' THEN 1 END) > 0
                THEN 'Both'
                WHEN COUNT(CASE WHEN Interest = 'Art' THEN 1 END) > 0
                THEN 'Art'
                ELSE 'Sport' END AS Interest
    FROM yourTable
    GROUP BY User
)

SELECT Interest, COUNT(*) AS Count
FROM cte
GROUP BY Interest;

在 MySQL 或 BigQuery 上,我们可以将上面的内容缩短为:

WITH cte AS (
    SELECT User,
           CASE WHEN SUM(Interest = 'Art') > 0 AND SUM (Interest = 'Sport') > 0
                THEN 'Both'
                WHEN SUM(Interest = 'Art') > 0
                THEN 'Art'
                ELSE 'Sport' END AS Interest
    FROM yourTable
    GROUP BY User
)

SELECT Interest, COUNT(*) AS Count
FROM cte
GROUP BY Interest;

假设您的数据库支持 over() 子句:

select
      case when num_interests = 1 then interest else 'both' end as interest
    , count(distinct user) as "Count"
from (
    select
      interest
    , user
    , count(*) over(partition by user) as num_interests
    from yourTable
    ) d
group by
      case when num_interests = 1 then interest else 'both' end