我有一张看起来像这样的表:
| user_id | client_application_id | invalidated_at
| 1 | 55555 | |
| 1 | 123 | |
我希望能做一件事:
我当前(不正确)的查询如下所示:
select user_id, count(user_id) as cnt from tokens where invalidated_at is null group by user_id having count(user_id) > 250 order by cnt ;
输出如下:
user_id | cnt
------------+-----
1 | 251
5 | 251
使用这个例子,我希望查询计算具有251的两个用户,所以它看起来像这样:
Count_Of_Users | Application_Count
2 | 251
答案 0 :(得分:1)
您可以使用与查询类似的内容作为子查询,并获得所需的输出:
select count(user_id) as count_of_users, cnt as application_count from
(
select user_id, count(client_application_id) as cnt
from tokens
where invalidated_at is null
group by user_id having count(client_application_id) > 250
) t
group by cnt
order by cnt
答案 1 :(得分:0)
不清楚secondo查询的预期结果是什么,可能是像这样的问题
select users.*, t.user_id, t.cnt
from users
inner join (
select user_id, count(user_id) as cnt
from tokens
where invalidated_at is null
group by user_id
having count(user_id) > 250
) t on users.id = t.user_id
order by t.user_id