0

Currently have a query that hits AWS Redshift. I have this group by a recipient in order to count the total of the output.

select count(CASE WHEN event_name = 'c' THEN 1 END) 
from oe
where owner_id = $1
and rid = $2
and cid = $2
and rbid is not null 
group by recipient 
having count(CASE WHEN event_name = 'c' THEN 1 END) > 0
and count(CASE WHEN event_name = 'd' THEN 1 END) > 0

The output from that is this

5
4
1
1

I'd like to be able, to sum up those numbers to produce a final number, as well as count the number of rows. How can I go about achieving this?

1
  • putting whole query into sum will sum up all the resulting numbers and output the final summed number. Commented Aug 31, 2020 at 18:33

1 Answer 1

1

This should give you the desired result.

select sum(sum_c), count(*) from (
 select count(CASE WHEN event_name = 'c' THEN 1 END) sum_c
from oe
where owner_id = $1
and rid = $2
and cid = $2
and rbid is not null 
group by recipient 
having count(CASE WHEN event_name = 'c' THEN 1 END) > 0
and count(CASE WHEN event_name = 'd' THEN 1 END) > 0) x;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.