postgresql - How to sum rows in groups then filter rows based on the sum -
i want find sum , count of positive values in table grouped type
column. interested in cases sum >= 10
, count >= 2
for example,
type | value ------+------- | 10 b | 5 c | 7 b | 5 c | 6 c | -1 d | 3 d | 4
i want result
type | sum | count ------+------+------ b | 10 | 2 c | 13 | 2
there should not row a
because count
1
. there should not row d
because sum
7
. negative value
should ignored.
i think answer should like:
select type, sum(value) sum, count(value) count my_table value > 0 group type having sum >= 10 , count >= 2
however not sure how correctly combine of relevant conditions.
select type, sum(value) sum, count(type) count my_table value > 0 group type having sum(value) >= 10 , count(type) >= 2
Comments
Post a Comment