SQL GROUP BY 詳解及復雜實例。本站提示廣大學習愛好者:(SQL GROUP BY 詳解及復雜實例)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL GROUP BY 詳解及復雜實例正文
GROUP BY 語句用於結合 Aggregate 函數,依據一個或多個列對後果集停止分組。
SQL GROUP BY 語法
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
演示數據庫
在本教程中,我們將運用眾所周知的 Northwind 樣本數據庫。
上面是選自 "Orders" 表的數據:
選自 "Shippers" 表的數據:
選自 "Employees" 表的數據:
SQL GROUP BY 實例
如今我們想要查找每個送貨員配送的訂雙數目。
上面的 SQL 語句按送貨員停止訂單分類統計:
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID GROUP BY ShipperName;
GROUP BY 一個以上的列
我們也可以對一個以上的列使用 GROUP BY 語句,如下所示:
SELECT Shippers.ShipperName, Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM ((Orders INNER JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID) INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID) GROUP BY ShipperName,LastName;
感激閱讀,希望能協助到大家,謝謝大家對本站的支持!