在group by 中使用HAVING篩選結果

來源:互聯網
上載者:User

在group by 中使用HAVING篩選結果

【例31】查詢所有價格超過10美元的書的種類和平均價格。

use   pubs

go  

select   type , avg(price) 'avg_price'  from    titles  where   price>$10   group   by   type

go

 

 

查詢結果如下:

Type                    avg_price            

 ------------ ------------------

Business                17.3100

mod_cook           19.9900

popular_comp 21.4750

Psychology          17.5100

trad_cook          15.9633

  (所影響的行數為 5 行)

 

【例32】在所有價格超過10美元的書中,查詢所有平均價格超過18美元的書的種類和平均價格。

use pubs

go  

select  type , avg(price) 'avg_price' from  titles where  price>10 group   by  type having  avg(price)>$18

go

查詢的結果是:

Type                                avg_price

------------ ---------------------

mod_cook                       19.9900

popular_comp       21.4750

(所影響的行數為 2 行)

WHERE子句在求平均值之前從表中選擇所需要的行,HAVING子句在進行統計計算後產生的結果中選擇所需要的行

 

指定組或彙總的搜尋條件。HAVING 通常與 GROUP BY 子句一起使用。如果不使用 GROUP BY 子句,HAVING 的行為與 WHERE 子句一樣。     

文法:   [HAVING <search_condition>]    

參數:    <search_condition>: 指定組或彙總應滿足的搜尋條件。當 HAVING 與 GROUP BY ALL 一起使用時,HAVING 子句替代 ALL。 在 HAVING 子句中不能使用 text、image 和 ntext 資料類型。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.