OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10248 | 90 | 5 | 1996-07-04 | 3 |
10249 | 81 | 6 | 1996-07-05 | 1 |
10250 | 34 | 4 | 1996-07-08 | 2 |
選自 "Shippers" 表的數(shù)據(jù):
ShipperID | ShipperName | Phone |
---|---|---|
1 | Speedy Express | (503) 555-9831 |
2 | United Package | (503) 555-3199 |
3 | Federal Shipping | (503) 555-9931 |
選自 "Employees" 表的數(shù)據(jù):
EmployeeID | LastName | FirstName | BirthDate | Photo | Notes |
---|---|---|---|---|---|
1 | Davolio | Nancy | 1968-12-08 | EmpID1.pic | Education includes a BA.... |
2 | Fuller | Andrew | 1952-02-19 | EmpID2.pic | Andrew received his BTS.... |
3 | Leverling | Janet | 1963-08-30 | EmpID3.pic | Janet has a BS degree.... |
SQL GROUP BY 實例
現(xiàn)在我們想要查找每個送貨員配送的訂單數(shù)目。
下面的 SQL 語句按送貨員進行訂單分類統(tǒng)計:
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;
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
標簽:貴州 湖州 內(nèi)蒙古 天津 成都 臨沂 常德 重慶
巨人網(wǎng)絡通訊聲明:本文標題《SQL GROUP BY 詳解及簡單實例》,本文關鍵詞 SQL,GROUP,詳解,及,簡單,實例,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。