我正在尝试显示某段时间内的报价数量,将每个报价的总和相加,并由创建报价的人对它们进行分组。以下是我的查询...但我认为我正在对该组做错事,但我不知道是什么。我正在使用标准的SQL 2008.非常感谢任何帮助。
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM
Quotes
WHERE
quotes.created>=? AND quotes.created<=?
Group by 1
答案 0 :(得分:2)
您的错误是由于您按聚合分组。您可以按列编号进行分组,但第一列包含count()。
您可以将1更改为3,也可以使用列名。
答案 1 :(得分:1)
您需要在分组
中使用createdby
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby FROM Quotes
WHERE
quotes.created>=? AND quotes.created<=?
Group by createdBy
答案 2 :(得分:0)
在GROUP BY
子句中,您必须包含SELECT
子句中的列。
如果您想根据谁创建预测来汇总预测,您应该像@Sachin和@Dan Bracuk那样推荐:
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM quotes
WHERE
quotes.created>=? AND quotes.created<=?
GROUP BY quotes.createdby
或
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM quotes
WHERE
quotes.created>=? AND quotes.created<=?
GROUP BY 3 --equivalent because createdBy is the 3º column on the select clause