I can't make my query work the way I need to. I have a simple query which outputs the following data:
我无法按照我需要的方式进行查询。我有一个简单的查询,输出以下数据:
What I need is to SUM the total_reseller_sales for each month and not display the same month twice. I try the following but it throws an error
我需要的是每月汇总total_reseller_sales而不是两次显示同一个月。我尝试以下但它会引发错误
SELECT rs.resellerid, rs.sid, SUM(rs.total_reseller_sales), s.month, s.sid
FROM sales_report_resellers rs
INNER JOIN resellers r ON rs.resellerid = r.resellerid
INNER JOIN sales_report s ON rs.sid = s.sid
WHERE (rs.sid > '294' AND rs.sid < '306') AND r.insidesales = 0
AND r.resellerid IN (7, 18, 22)
GROUP BY month
The error I get is that each element in SELECT is invalid because it is not contained in either an aggregate function or the GROUP BY clause.
我得到的错误是SELECT中的每个元素都是无效的,因为它不包含在聚合函数或GROUP BY子句中。
If I include them i nthe GROUP BY, then I get the same results.
如果我将它们包含在GROUP BY中,那么我会得到相同的结果。
Any help would be appreciated.
任何帮助,将不胜感激。
1 个解决方案
#1
3
The GROUP BY clause forms rows for each unique combinaton of the columns you nominate in that clause.
GROUP BY子句为您在该子句中指定的列的每个唯一组合形成行。
If you want to show a sum for each month, then only include s.month in the group by clause: e.g
如果要显示每个月的总和,则只在group by子句中包含s.month:例如
SELECT s.month, SUM(rs.total_reseller_sales)
FROM sales_report_resellers rs
INNER JOIN resellers r ON rs.resellerid = r.resellerid
INNER JOIN sales_report s ON rs.sid = s.sid
WHERE (rs.sid > '294' AND rs.sid < '306') AND r.insidesales = 0
AND r.resellerid IN (7, 18, 22)
GROUP BY s.month
If you include reseller details in the select clause also include them in the group by clause then there will be one row per reseller AND month
如果您在select子句中包含转销商详细信息,也将它们包含在group by子句中,那么每个转销商和每月将有一行
#1
3
The GROUP BY clause forms rows for each unique combinaton of the columns you nominate in that clause.
GROUP BY子句为您在该子句中指定的列的每个唯一组合形成行。
If you want to show a sum for each month, then only include s.month in the group by clause: e.g
如果要显示每个月的总和,则只在group by子句中包含s.month:例如
SELECT s.month, SUM(rs.total_reseller_sales)
FROM sales_report_resellers rs
INNER JOIN resellers r ON rs.resellerid = r.resellerid
INNER JOIN sales_report s ON rs.sid = s.sid
WHERE (rs.sid > '294' AND rs.sid < '306') AND r.insidesales = 0
AND r.resellerid IN (7, 18, 22)
GROUP BY s.month
If you include reseller details in the select clause also include them in the group by clause then there will be one row per reseller AND month
如果您在select子句中包含转销商详细信息,也将它们包含在group by子句中,那么每个转销商和每月将有一行