Zeratas
Zeratas

Reputation: 1015

MySql SUM function combined with where conditional

This is the first part of a crazy query I have to put together so expect another question on here soon!

I have this table Table Description

What I am trying to do is to get a list of CONTEXT that have a SUM(FILE_SIZE) greater than 5 gigs.

I have every part of the query figured out except putting in the where clause.

SELECT CONTEXT,SUM(FILE_SIZE) AS size FROM CONTENT_RESOURCE GROUP BY CONTEXT.

What I'm having trouble putting together is something like:

WHERE SUM(FILE_SIZE) > 5000000000

This would be done for each unique CONTEXT.

So if there is data like this:

1 | A
4 | A
2 | C
6 | C
8 | B

The result would be something like:

5 | A
8 | C
8 | B

So I need to get the SUM(FILE_SIZE) for all of the different CONTEXTs and then only return the ones where it is greater than 5000000000

Upvotes: 1

Views: 34

Answers (1)

Mureinik
Mureinik

Reputation: 311823

The having clause allows you to apply conditions on groups (and aggregate results) created by the group by clause:

SELECT   context, SUM(file_size) AS size 
FROM     content_resource
GROUP BY context
HAVING   SUM(file_size) > 5000000000

Upvotes: 2

Related Questions