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