views:

118

answers:

1

OK, so I have two tables I'm working with - project and service, simplified thus:
project
-------
id PK
name str

service
-------
project_id FK for project
time_start int (timestamp)
time_stop int (timestamp)

One-to-Many relationship.

Now, I want to return (preferably with one query) a list of an arbitrary number of projects, sorted by the total amount of time spent at them, which is found by SUM(time_stop) - SUM(time_start) WHERE project_id = something.

So far, I have
SELECT project.name
FROM service
LEFT JOIN project ON project.id = service.project_id
LIMIT 100

but I cannot figure out how what to ORDER BY.

+3  A: 

You need a GROUP BY:

SELECT project.name
FROM service
LEFT JOIN project ON project.id = service.project_id
GROUP BY project_id
ORDER BY SUM(time_stop - time_start)
LIMIT 100
Mark Byers
that should be `GROUP BY project.name`, and contrary to the actual question i think he really wants `SUM(time_stop - time_start)`
Gaby
@Gaby: Since it is a 1-1 mapping I would think that in MySQL it makes no difference which field you choose to group by.
Mark Byers
@Gaby: I agree that `SUM(time_stop - time_start)` is more readable and reduces the risk of overflow - I'll fix that.
Mark Byers
@mark, my understanding is that it is a requirement that whatever you `select` you must put in the `group by` unless they are aggregated results.. (but i may be thinking in ms-sql-server requirements..)
Gaby
`GROUP BY project.id` seems more lightweight. Either way, you both rock. Incidentally, I found it out online about the same time as you posted, heh. I should learn to do that before wasting you guys' time.Thanks!
Christian Mann
@Gaby: I don't think that it's a requirement in MySQL. "MySQL extends the use of GROUP BY so that you can use nonaggregated columns or calculations in the SELECT list that do not appear in the GROUP BY clause." See: http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html
Mark Byers
@Mark, fair enough (i was thinking in ms-sql-server requirements) and thanks for the link :)
Gaby
Actually, I think the important part to remember is this: "When using this feature, all rows in each group should have the same values for the columns that are ommitted from the GROUP BY part. The server is free to return any value from the group, so the results are indeterminate unless all values are the same."
Daniel Vassallo
@Gaby: You're welcome. Thanks for helping out. :)
Mark Byers
@Daniel: Yes, you're right - but in this case since project.id is a PK, this should be safe and possibly more efficient than grouping on the name (and it won't fail if two different projects have the same name).
Mark Byers
Yes, good point... +1 for the quick solution.
Daniel Vassallo