close

這是線上平台課程的筆記內容,就是讓自己方便複習用。

老師講課的進度掌握得很好,教得也很淺寫易懂。

線上平台課程內有提供 SQL套件,如果有興趣的朋友一定要去支持老師的課程。

這樣才能取得老師提供的套件包做練習使用。

如果有想要知道老師的課程,再詢問我。

 


CH7  Group BY 以及 Unions

 

將查詢結果當中的特定欄位值,其中的資料依相同與否分成若干群組。

每個群組都只會回傳一個資料列。

 

如果沒有使用GROUP BY,針對SELECT查詢聚合函數時,只能回傳一個值。

聚合函數 COUNT() MAX() MIN() SUM() AVG()

 

SELECT 欄位

FROM table

WHERE 條件

GROUP BY 欄位名稱(s)

ORDER BY 欄位名稱(s);

 

我們在每個國家/地區 有多少的客戶

SELECT country, COUNT(*)

FROM customers

GROUP BY country

ORDER BY COUNT(*) DESC;

 

!!因為要讓多個資訊有聚合函數的效果

所以這此挑countryGROUP BY來分組,

最後在SELECT時然後用聚合函數COUNT(*)來計算。

 

每個類別產品數量是多少

如果只有類別的話,每個項目都只會有一條目。

SELECT categoryname, COUNT(*)

FROM categories

GROUP BY categoryname

ORDER BY COUNT(*) DESC;

 

如果要將這個列裡面有多少產品算出來,

就要先把products這張表INNER JOIN進來。

因為這張表才有每個 類別 的產品清單。

不直接去product那邊撈資料是因為,那邊的category是用ID標示。

也帶不出甚麼東西。

 

SELECT categoryname, COUNT(*)

FROM categories

INNER JOIN products ON products.categoryid = categories.categoryid

GROUP BY categoryname

ORDER BY COUNT(*) DESC;

 

 

訂單中產品的平均數量。

SELECT productname, AVG(quantity)

FROM products

INNER JOIN order_details ON order_details.productid = products.productid

GROUP BY productname

ORDER BY AVG(quantity) DESC;

 

數量要從order_details表單中取得,

商品名稱要從prdoucts裡面取得。

 

LAB 1997年銷售的每件商品的總價值是多少

單一件訂單就是unitprice*quantity,要加總就要用SUM

但這邊沒有辦法顯示購買年份。

SELECT productname ,SUM (unitprice*quantity)

FROM products

INNER JOIN order_details ON order_details.productid = products.productid

GROUP BY productname

ORDER BY SUM (unitprice*quantity) DESC;

 

把有購買年份的資料拉進來。

SELECT productname ,SUM(unitprice*quantity)

FROM products

INNER JOIN order_details ON order_details.productid = products.productid

INNER JOIN orders ON orders. orderid = order_details.orderid

WHERE orderdate BETWEEN '1997-01-01' AND '1997-12-31'

GROUP BY productname

ORDER BY SUM (unitprice*quantity) DESC;

 

這邊得到錯誤,因為unitprice這個數據,在order_details & products裡面都有。

所以這邊還在指定。

 

SELECT productname ,SUM(order_details.unitprice* order_details.quantity)

FROM products

INNER JOIN order_details ON order_details.productid = products.productid

INNER JOIN orders ON orders. orderid = order_details.orderid

WHERE orderdate BETWEEN '1997-01-01' AND '1997-12-31'

GROUP BY productname

ORDER BY SUM(order_details.unitprice* order_details.quantity) DESC;

這樣時間區間就解決了。

 


HAVING 語法

 

SELECT 欄位

FROM

WHERE 條件

GROUP BY 欄位名稱

HAVING 條件

ORDER BY 欄位名稱

 

簡單說,HAVING是取代WHERE 來搭配聚合函數進行查詢的

如果沒有要用到聚合函數,那搭配WHERE還是OK的。

因為WHERE沒有辦法跟聚合函數一起使用。

聚合函數 COUNT() MAX() MIN() SUM() AVG()...

 

EX:

SELECT customer,SUM(unitprice * quantity)

FROM customers

INNER JOIN order_details ON ….

GROUP BY customer

HAVING SUM(unitprice * quantity) < 100;

類似這樣的用法。

 

 

找到售價低於2000元的產品。

SELECT productname ,SUM(order_details.unitprice* order_details.quantity)

FROM products

INNER JOIN order_details ON order_details.productid = products.productid

INNER JOIN orders ON orders. orderid = order_details.orderid

GROUP BY productname

HAVING SUM(order_details.unitprice* order_details.quantity) >2000

ORDER BY SUM(order_details.unitprice* order_details.quantity) DESC;

 

這邊順序要注意

HAVING SUM(order_details.unitprice* order_details.quantity) >2000

GROUP BY productname

 

放反的話會出現錯誤。

GROUP BY productname

HAVING SUM(order_details.unitprice* order_details.quantity) >2000

 

利用別名的方式也會錯誤

HAVING適用於聚合函數。

 

LAB 找尋購買超過5000元的客戶。

SELECT companyname,SUM(order_details.unitprice* order_details.quantity)

FROM customers

INNER JOIN orders ON orders.customerid = customers.customerid

INNER JOIN order_details ON order_details.orderid = orders.orderid

GROUP BY companyname

HAVING SUM(order_details.unitprice* order_details.quantity) > 5000

ORDER BY SUM(order_details.unitprice* order_details.quantity) DESC;

 

LAB 要求增加WHERE使用。

金額超過5000元,且下訂日期要在1997的上半年。

 

記得先把要求篩選掉。

SELECT companyname,SUM(order_details.unitprice* order_details.quantity)

FROM customers

INNER JOIN orders ON orders.customerid = customers.customerid

INNER JOIN order_details ON order_details.orderid = orders.orderid

WHERE orderdate BETWEEN '1997-01-01' AND '1997-06-30'

GROUP BY companyname

HAVING SUM(order_details.unitprice* order_details.quantity) > 5000

ORDER BY SUM(order_details.unitprice* order_details.quantity) DESC;

 


UNION 聯盟

 

SELECT 欄位

FROM table

UNION

SELECT 欄位

FROM table

 

讓你抓取更多的資料回來!?讓兩個查詢結合再一起。

!!使用UNION時,每一列的數據要有相同的數據類型資料

!!SELECT 當中必須擁有相同數量的列。

 

來練習擷取所有客戶&供應商名稱列表。

這邊的查詢包含了所有的客戶 & 供應商

 

但是只有UNION的狀況下,重複的東西他會不讓它顯示(重複的就不再出現)

SELECT companyname

FROM customers

UNION

select companyname

FROM suppliers

UNION ALL就是管他重不重複,就都出現。

 

這邊的練習是搜尋所有的城市,包含客戶&供應商的重複。

先看UNION

SELECT city

FROM customers

UNION

SELECT city

FROM suppliers

 

 

SELECT city

FROM customers

UNION ALL

SELECT city

FROM suppliers

這邊就可以看到,重不重複都列出來。

 

 

LAB 搜尋客戶&供應商 的所有國家 (不重複)

 

SELECT country

FROM customers

UNION

SELECT country

FROM suppliers

 

 

最後再來一下依照順序排列。

位置放置錯誤,OREDE BY 基本上就是最後排序了

 

放到最後依照查詢排序就正常。

排序順序就是依照全部查詢完的結果來進行。

反正都是查詢country

LAB2 想要一份來自供應商&客戶的國家清單

並列出每個國家。

(供應商的國家這個狀況一定會有重複的,所以要用ALL)

加上ALL就完成了。

<本篇完>


第一篇:  IT讀書室-SQL語法學習- postgre安裝與匯入

第二篇:  IT讀書室-SQL語法學習- SELECT查詢

第三篇:  IT讀書室-SQL語法學習- WHERE條件

第四篇:  IT讀書室-SQL語法學習- 中間的SELECT語句

 


如果筆記造成任何法律或者侵權的問題,作者會立即將筆記下架。

arrow
arrow
    全站熱搜

    IT001 發表在 痞客邦 留言(0) 人氣()