Home > OS >  Select distinct results of sum
Select distinct results of sum

Time:07-09

I've been trying to find some help on this but can't seem to find any. I have data like this on my table:

ID ratios
1111 0,004
2222 0,023
2222 0,012
3333 0
4444 0
... ...

For every ID I have one or more ratio values. And my next step is to sum them all.

select ID, sum(ratios)
from table
group by ID;

Resulting in something like this:

ID sum(ratios)
1111 1
2222 1
3333 0
4444 0
... ...

Here's where I hit a wall. My goal is to check if any of the sum results goes over 1 to validate my ratios. I was trying to select distinct the sum results but can't make it work since I'm also working with the group by clause. Can someone you help me solve this?

CodePudding user response:

select ID, sum(ratios)
from table
group by ID
having sum(ratios) > 1

Having is like a where clause for group by.

CodePudding user response:

If you use a having statement, which is like a WHERE statement for your aggregations, you can return all of your sums that are greater than 1.

select ID, sum(ratios)
from table
group by ID
having sum(ratios) > 1;
  • Related