views:

293

answers:

4

I'm trying to get the MAX on a column which is generated dynamically using the SUM statement. The SUM statement is used together with the 'GROUP by' syntax.

This is the original query, however it needs to be modified to work with grouping, sums and of course MAX.

SELECT  SUM(video_plays) AS total_video_plays
    FROM `video_statistics` v_stat
    GROUP BY v_stat.`video_id` ASC

As you can see SUM is adding all the values inside video_plays as *total_video_plays*..

But I SIMPLY want to get the MAX of *total_video_plays*

My attempts are below, however they do not work..

SELECT SUM(video_plays) AS MAX(total_video_plays)
    FROM `video_statistics` v_stat
    GROUP BY v_stat.`video_id` ASC

How would you get the MAX on a column made dynamically without using sub quieres - Because the above is already placed within one.

A: 

can't you just do this?:

SELECT video_id, max(video_plays) AS max_video_plays    
FROM `video_statistics` v_stat    
GROUP BY v_stat.`video_id` ASC

There's an example here:

http://dev.mysql.com/doc/refman/5.1/de/select.html

SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) > 10;

EDIT: second attempt, albeit using a subquery:

select max(sum_video_plays)
from (
SELECT video_id, sum(video_plays) AS sum_video_plays    
FROM `video_statistics` v_stat    
GROUP BY v_stat.`video_id`
) x

Your outer query may well be selecting from a much smaller set, and (depending on your data distribution etc.) may be quite performant.

davek
The first query will give you the highest single entry, not the highest sum of.
OMG Ponies
@Dave: No worries, it's possible MySQL's `GROUP BY` will work, but it's frustrating because the syntax isn't supported by any other DB I'm aware of.
OMG Ponies
+2  A: 

You can not do what you're asking without a subquery, because you can't run two aggregate functions, one on top of the other.

OMG Ponies
Would it have a significant effect on performance? Iv read sub-queries can bog down your resources How then, would I do so using a sub query?
BeaversAreDamned
+1 for not misunderstanding the question, as I did!
davek
@Beavers: Subqueries on MySQL don't perform well, but that's not the case for all databases.
OMG Ponies
Just how bad ARE subqueries in MySQL? My natural inclination (coming from databases in which they're quite fast) is to use a subquery instead of a JOIN if it better expresses the intent of the query — I gather that's not done in MySQL?
Larry Lustig
OMG Ponies
I just hate the idea that sub-queries (a basic design element of SQL) have become "a necessary evil" due, essentially, to MySQL's failure at first to implement them at all and later to implement them properly.
Larry Lustig
@Larry: It's a reflection of other peoples experience - I'd think that way too if I never used Oracle/SQL Server/etc. And every db has its quirks, though MySQL's are more negative than any other I've used (looking at you, `GROUP BY`...)
OMG Ponies
+3  A: 

Something like

SELECT SUM(video_plays) AS total_video_plays
FROM `video_statistics` v_stat
GROUP BY v_stat.`video_id` 
ORDER BY total_video_plays DESC 
LIMIT 1

Hat Tip OMG Ponies for proper MySQL dialect.

Larry Lustig
Thanks, since I have the SQL in question nested about 5 times. I think I've gone way past the 'bad practice' line. That's why Im trying to keep subs to a minimal. This does the trick. Thank you yet again! Larry.
BeaversAreDamned
A: 

Will this work for you?

SELECT MAX(total_video_plays) from table (
 SELECT SUM(video_plays) AS total_video_plays
    FROM `video_statistics` v_stat
    GROUP BY v_stat.`video_id` ASC )

It contains a subquery, but maybe not in the sense you were thinking.

Wes
I don't think that would work as is - I've only done it as FROM (SELECT...`, but that is the only way to do it using the two aggregate functions.
OMG Ponies
Thanks for your attempt! Appreciate it.
BeaversAreDamned