I have a mytable
in AWS Redshift with user_id
and records
. I need to count number of records for each user but only if ALL records for this user are greater than a threshold.
mytable:
user_id | records
------------------
0 | 678
0 | 567
1 | 845
1 | 123
1 | 420
2 | 789
Threshold = 400 so the result should be:
user_id | count
------------------
0 | 2
2 | 1
I will appreciate your help!
You can use group by
with having
:
select user_id, sum(case when records > 400 then 1 else 0 end) as cnt
from t
group by user_id
having min(records) > 400;
Or using ::
:
select user_id, sum( (records > 400)::int ) as cnt
from t
group by user_id
having min(records) > 400;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.