2013-11-20 138 views
0
   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. 
+0

爲什麼將數值與字符串進行比較?例如,'COUNT'返回一個'INT',並將它與'1'比較而不是'1'是特有的。比較空的字符串更奇怪。 – HABO

回答

2

使用

HAVING COUNT(EmailOfConsumer) = 1 

having子句限制了聚合,而只能在where子句出臺限制有關個人列數據

+0

或者添加「HAVING TotalValue/AverageValue = 0」。順便說一句:我建議不要使用'1'作爲值,而是使用實數1(無'),否則MySQL會投射字符串,效率會降低。此外,HAVING非常慢,在查詢完成處理後它可以工作 - 所以使用它被認爲是不好的做法。 – nrathaus

+1

在GROUP BY子句之後添加HAVING子句並在ORDER BY –

+0

之前添加HAVING子句@JackJiang只有在值不爲空時才知道如何添加此HAVING子句? – neeko

1
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 

HAVING COUNT(EmailOfConsumer) = '1' 
       ORDER BY TotalValue DESC 
1

您可以嘗試

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 
      HAVING COUNT(EmailOfConsumer) = 1 
      ORDER BY TotalValue DESC 
1

您需要添加過濾條件在HAVING clause也算回報率的數值,以便無需添加引號,以檢查它

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 
      HAVING COUNT(EmailOfConsumer) = 1 
      ORDER BY TotalValue DESC 

你不能COUNT in WHERE clause檢查條件,因爲它會聚集之前執行,所以你需要聚集後,檢查它HAVING clause

1

使用此查詢:

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 
HAVING COUNT(*) = 1 
ORDER BY TotalValue DESC 

注:

  • WHERE條款之前GROUP BY,而HAVING條款如下GROUP BY
  • WHERE條款一個過濾器之前行ggregation; HAVING子句過濾器彙總行
  • COUNT(...)返回一個數字,所以常量1應該而不是被括在引號中。
2

使用group by,然後使用子句。請參閱this

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 
      HAVING COUNT(EmailOfConsumer) = '1' 
      ORDER BY TotalValue DESC