MySQL sum() and group by
I have the following query:
SELECT
corporation.id, expense.year, expense.type, SUM(Expense.amount) as sum
FROM
expenses 开发者_如何转开发
LEFT JOIN
corporation ON expense.corporation_id = corporation.id
LEFT JOIN
project ON expense.project_id = project.id
WHERE
project.id = XYZ
GROUP BY
corporation.name, corporation.id, expense.year
ORDER BY
sum DESC;
MySQL now orders this by sum
for the latest expense.year, but I'd like it to order by sum
of all years (i.e. like a query without GROUP BY (...) expense.year
). Is this possible?
Sum is a single value so you can get it using one query. And get other data in seperate query. or use a sub query at SUM(...) as sum
精彩评论