MySQL: Grouping results
How can I group my results in a query such as this:
SELECT (
SELECT SUM(wky) FROM table
WHERE Earnings_Date BETWEEN '2011-08-14' AND '2011-08-16'
) +
(SELECT SUM(earnings) FROM table
WHERE Earnings_Date BETWEEN '2011-08-14' AND '2011-08-16') / .75
The above query returns wky+(earnings/.75) for all the sites (each site has a wky and earnings number for different dates. Where would I put a group by in the above query? I would like my r开发者_如何学Goesults to be grouped by site_id, since the above query just returns the sum of the earnings of all of them.
Thanks!
SELECT site_id, (SUM(wky) + (SUM(earnings) / 0.75))
FROM Earnings_Date
WHERE Earnings_Date BETWEEN '2011-08-14' AND '2011-08-16'
GROUP BY site_id
Your data comes from the same table for both queries, so you can combine them.
SELECT (
SELECT SUM(wky) FROM table
WHERE Earnings_Date BETWEEN '2011-08-14' AND '2011-08-16'
) +
(SELECT SUM(earnings) FROM table
WHERE Earnings_Date BETWEEN '2011-08-14' AND '2011-08-16') / .75
, site_id GROUP BY site_id
:)
精彩评论