试图弄清楚如何做到以下几点:
sum(case when count(goal_id) over (partition by user_id) > 1 then 1 else 0 end) as user_with_multiple_goals
。显然不支持此功能-但我不希望以任何方式不添加更多子查询。
有什么建议吗?
表格:
user_id goal_id
A A
A B
A C
B D
C E
C F
输出:
2
答案 0 :(得分:1)
根据您的示例数据,可以将其简化为正常情况/计数:
SELECT user_id,
CASE WHEN COUNT(*) > 1
THEN 1
ELSE 0
END as user_with_multiple_goals
FROM yourTable
GROUP BY user_id
发布修改:
SELECT DISTINCT COUNT(*)
FROM yourTable
GROUP BY user_id
HAVING COUNT(*) > 1
答案 1 :(得分:0)
我在想:
select count(*)
from (select user_id, count(*) as num_goals
from t
group by user_id
) u
where num_goals > 1;
或者:
select count(distinct user_id)
from t
where exists (select 1 from t t2 where t2.user_id = t.user_id and t2.goal_id <> t.goal_id);