關於SQLServer2005的學習筆記——子查詢
SQL Server的子查詢給人的感覺一向不是很好用,IN子查詢無法實現多列的子查詢,很多情況下又需要進行自我的子查詢操作,比如取員工的最新訂單之類的問題。
以下SQL和案例來之於
讓我們先來看看Oracle是怎麼處理子查詢的
CREATE TABLE Orders ( OrderID VARCHAR2(6), CustomerID VARCHAR2(6), EmployeeID INT, OrderDate DATE ); TRUNCATE TABLE Orders; INSERT INTO Orders VALUES('110001','WBQ',1,TO_DATE('2000-01-11','YYYY-MM-DD')); INSERT INTO Orders VALUES('110002','WBQ',1,TO_DATE('2000-01-21','YYYY-MM-DD')); INSERT INTO Orders VALUES('110003','WBQ',2,TO_DATE('2000-01-11','YYYY-MM-DD')); INSERT INTO Orders VALUES('110004','WBQ',3,TO_DATE('2000-02-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110005','CZH',1,TO_DATE('2000-02-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110006','CZH',2,TO_DATE('2000-03-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110007','CZH',2,TO_DATE('2000-03-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110008','KIDD',3,TO_DATE('2000-02-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110009','KIDD',1,TO_DATE('2000-04-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110010','KIDD',2,TO_DATE('2000-03-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110011','KIDD',3,TO_DATE('2000-02-01','YYYY-MM-DD')); INSERT INTO Orders VALUES('110012','CZH',3,TO_DATE('2000-01-10','YYYY-MM-DD')); COMMIT; SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders ORDER BY EmployeeID,OrderDate,OrderID |
--層遞直至實現唯一為止 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders WHERE (EmployeeID,OrderDate,OrderID) IN (SELECT EmployeeID,OrderDate,MAX(OrderID) FROM Orders WHERE (EmployeeID,OrderDate) IN (SELECT EmployeeID,Max(OrderDate) FROM Orders GROUP BY EmployeeID) GROUP BY EmployeeID,OrderDate) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
--多值,通常情況下該語句即可,不過在本例中EmployeeID,OrderDate並不唯一,導致結果非所需的 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders WHERE (EmployeeID,OrderDate) IN (SELECT EmployeeID,Max(OrderDate) FROM Orders GROUP BY EmployeeID) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
--Error,這是個錯誤的表示式 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders WHERE (EmployeeID,OrderDate,OrderID) IN (SELECT EmployeeID,MAX(OrderDate),MAX(OrderID) FROM Orders GROUP BY EmployeeID) |
--使用分析函式,也可以實現相應的子查詢 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM ( SELECT EmployeeID,OrderDate,OrderID,CustomerID, RANK() OVER (PARTITION BY EmployeeID ORDER BY OrderDate DESC,OrderID DESC) Rank FROM Orders ) a WHERE a.Rank=1
|
讓我們繼續看看SQL Server是如何處理的
CREATE TABLE Orders ( OrderID VARCHAR(6), CustomerID VARCHAR(6), EmployeeID INT, OrderDate DATETIME ); INSERT INTO Orders VALUES('110001','WBQ',1,'2000-01-11'); INSERT INTO Orders VALUES('110002','WBQ',1,'2000-01-21'); INSERT INTO Orders VALUES('110003','WBQ',2,'2000-01-11'); INSERT INTO Orders VALUES('110004','WBQ',3,'2000-02-01'); INSERT INTO Orders VALUES('110005','CZH',1,'2000-02-01'); INSERT INTO Orders VALUES('110006','CZH',2,'2000-03-01'); INSERT INTO Orders VALUES('110007','CZH',2,'2000-03-01'); INSERT INTO Orders VALUES('110008','KIDD',3,'2000-02-01'); INSERT INTO Orders VALUES('110009','KIDD',1,'2000-04-01'); INSERT INTO Orders VALUES('110010','KIDD',2,'2000-03-01'); INSERT INTO Orders VALUES('110011','KIDD',3,'2000-02-01'); INSERT INTO Orders VALUES('110012','CZH',3,'2000-01-10'); COMMIT; |
--多值,通常情況下該語句即可,不過在本例中EmployeeID,OrderDate並不唯一,導致結果非所需的 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders O1 WHERE rderDate= (SELECT MAX(OrderDate) FROM Orders O2 WHERE O1.EmployeeID=O2.EmployeeID) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID 等效於Oracle的以下語句 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders WHERE (EmployeeID,OrderDate) IN (SELECT EmployeeID,Max(OrderDate) FROM Orders GROUP BY EmployeeID) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
正確的SQLServer子查詢寫法,用兩個MAX求得唯一值 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders O1 WHERE rderDate= (SELECT MAX(OrderDate) FROM Orders O2 WHERE O1.EmployeeID=O2.EmployeeID) AND rderID= (SELECT Max(OrderID) FROM Orders O2 WHERE O1.EmployeeID=O2.EmployeeID AND O1.OrderDate=O2.OrderDate) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
--本例使用一種變通的方法,把幾個應有的附加條件加進來然後返回,比較難以理解 SELECT CAST(SUBSTRING(BinStr,1,8) AS DATETIME) AS OrderDate, CAST(SUBSTRING(BinStr,9,6) AS VARCHAR) AS OrderID, CAST(SUBSTRING(BinStr,15,6) AS VARCHAR) AS CustomerID FROM (SELECT EmployeeID, MAX(CAST(OrderDate AS BINARY(8)) +CAST(OrderID AS BINARY(6)) +CAST(CustomerID AS BINARY(6))) AS BinStr FROM Orders GROUP BY EmployeeID) D; |
--本例中在子查詢中使用TOP+Order排序的方式獲取相應的第一行值 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders O1 WHERE rderID= (SELECT TOP(1) OrderID FROM Orders O2 WHERE O1.EmployeeID=O2.EmployeeID ORDER BY OrderDate DESC,OrderID DESC,CustomerID ) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
--本例使用了IN子查詢,可以自定義返回的TOP N條數 SELECT EmployeeID,OrderDate,OrderID,CustomerID FROM Orders O1 WHERE OrderID IN (SELECT TOP(1) OrderID FROM Orders O2 WHERE O1.EmployeeID=O2.EmployeeID ORDER BY OrderDate DESC,OrderID DESC,CustomerID ) ORDER BY EmployeeID,OrderDate,OrderID,CustomerID |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6517/viewspace-624818/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MYSQL學習筆記26: 多表查詢|子查詢MySql筆記
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- 關於MySQL8的WITH查詢學習MySql
- (MySQL學習筆記)分頁查詢MySql筆記
- Myth 關於Git的學習筆記Git筆記
- 關於http(自己的學習筆記)HTTP筆記
- oracle學習筆記(十一) 高階查詢Oracle筆記
- 第一個完整的spring查詢功能學習筆記【Spring工程學習筆記(二)】Spring筆記
- MySQL必知必會 學習筆記 第十四章 使用子查詢MySql筆記
- 資料庫學習筆記之查詢表資料庫筆記
- SpringBoot學習筆記13——MybatisPlus條件查詢Spring Boot筆記MyBatis
- mysql,where條件查詢等學習筆記MySql筆記
- MYSQL學習筆記24: 多表查詢(聯合查詢,Union, Union All)MySql筆記
- 區分關聯子查詢和非關聯子查詢
- MYSQL學習筆記6: DQL條件查詢(where)MySql筆記
- MYSQL學習筆記8: DQL分組查詢(group by)MySql筆記
- ES[7.6.x]學習筆記(十)聚合查詢筆記
- 資料庫學習(五)子查詢資料庫
- 關於django reset_framework學習之路的筆記DjangoFramework筆記
- MYSQL學習筆記11: DQL查詢執行順序MySql筆記
- Mybatis學習筆記 3:Mybatis 多種條件查詢MyBatis筆記
- MySQL 相關子查詢MySql
- 【學習】SQL基礎-007-子查詢SQL
- 關於網路安全的逆向分析方向學習筆記筆記
- XML學習筆記(一):關於字元編碼的理解XML筆記字元
- MyBatis學習筆記(四)使用map實現查詢和插入MyBatis筆記
- Linux 學習筆記--環境變數與檔案查詢Linux筆記變數
- 什麼是SQL 語句中相關子查詢與非相關子查詢SQL
- 學習 XQuery:XML資料查詢的關鍵XML
- ThinkPHP5.0 使用 model 關聯查詢筆記PHP筆記
- C#關於List<T>的自定義排序學習筆記C#排序筆記
- php 學習筆記之關於時區的那點事PHP筆記
- 一份關於 Java、Kotlin 與 Android 的學習筆記JavaKotlinAndroid筆記
- 【NLP學習筆記】(三)gensim使用之相似性查詢(SimilarityQueries)筆記MILA
- SQL查詢的:子查詢和多表查詢SQL
- 關於oracle的空間查詢Oracle
- MySQL資料庫學習筆記02(事務控制,資料查詢)MySql資料庫筆記
- sql-server不相關子查詢SQLServer
- sql-server相關子查詢SQLServer