SELECT
EmailOfConsumer,
COUNT(EmailOfConsumer) as 'NumberOfOrders',
SUM(CAST(Total as money)) as 'TotalValue',
(SUM(CAST(Total as money))/COUNT(EmailOfConsumer)) as 'AverageValue'
FROM webshop
GROUP BY EmailOfConsumer
ORDER BY TotalValue DESC
此带回:查询计数记录用WHERE子句
EmailOfConsumer NumberOfOrders TotalValue AverageValue
test 1 2000000000.10 2000000000.10
我想在WHERE NumberOfOrders = '1'
添加一个搜索我曾尝试加入WHERE COUNT(EmailOfConsumer) = '1'
,但我得到此错误:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a
HAVING clause or a select list, and the column being aggregated is an outer reference.
为什么将数值与字符串进行比较?例如,'COUNT'返回一个'INT',并将它与'1'比较而不是'1'是特有的。比较空的字符串更奇怪。 – HABO