如何在以下查询中对结果进行分组:
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
以上查询返回所有网站的wky +(收益/ .75)(每个网站都有不同日期的wky和收益编号。在上述查询中,我将在哪里放一个组?我希望我的结果是按site_id分组,因为上述查询只返回所有这些收入的总和。
谢谢!
答案 0 :(得分:2)
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
您的数据来自两个查询的同一个表,因此您可以将它们组合在一起。
答案 1 :(得分:-1)
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
:)