[英]T-SQL and/or LINQ query with multiple criteria
我有以下示例多對多關系:
我正在構建一個UI,以允許用戶選擇要一起批處理的多個文檔。 每個批次可以有很多文件; 除非該批次已作廢,否則每個文檔只能在一個批次中。
在此UI上,我需要顯示可用的文檔。 如果滿足以下條件,則認為文檔可用 :
樣本數據:
CREATE TABLE tmpDocument (DocumentID INT, IsVoid BIT)
INSERT INTO tmpDocument VALUES (1,1)
INSERT INTO tmpDocument VALUES (2,0)
INSERT INTO tmpDocument VALUES (3,0)
INSERT INTO tmpDocument VALUES (4,0)
INSERT INTO tmpDocument VALUES (5,0)
INSERT INTO tmpDocument VALUES (6,0)
INSERT INTO tmpDocument VALUES (7,0)
INSERT INTO tmpDocument VALUES (8,0)
INSERT INTO tmpDocument VALUES (9,0)
CREATE TABLE tmpBatch (BatchID INT, IsVoid BIT)
INSERT INTO tmpBatch VALUES (1,0)
INSERT INTO tmpBatch VALUES (2,1)
INSERT INTO tmpBatch VALUES (3,0)
CREATE TABLE tmpBatchDocument (BatchDocumentID INT, BatchID INT, DocumentID INT)
INSERT INTO tmpBatchDocument VALUES (1,1,2)
INSERT INTO tmpBatchDocument VALUES (2,1,3)
INSERT INTO tmpBatchDocument VALUES (3,2,4)
INSERT INTO tmpBatchDocument VALUES (4,2,5)
INSERT INTO tmpBatchDocument VALUES (5,3,6)
INSERT INTO tmpBatchDocument VALUES (6,3,7)
INSERT INTO tmpBatchDocument VALUES (7,3,8)
文獻
DocumentID---IsVoid
1------------1-----
2------------0-----
3------------0-----
4------------0-----
5------------0-----
6------------0-----
7------------0-----
8------------0-----
9------------0-----
批量
BatchID------IsVoid
1------------0-----
2------------1-----
3------------0-----
BatchDocument
BatchDocumentID-----BatchID------DocumentID
1-------------------1------------2---------
2-------------------1------------3---------
3-------------------2------------4---------
4-------------------2------------5---------
5-------------------3------------6---------
6-------------------3------------7---------
7-------------------3------------8---------
為了找到可用的文檔,我提出了以下T-SQL查詢:
SELECT a.DocumentID
FROM tmpDocument a
LEFT JOIN tmpBatchDocument b ON a.DocumentID = b.DocumentID
LEFT JOIN tmpBatch c ON b.BatchID = c.BatchID
WHERE (
b.DocumentID IS NULL
AND a.IsVoid = 0
)
OR (c.IsVoid = 1)
查詢正確返回:
DocumentID
4
5
9
排除了文檔1,因為它已作廢; 排除了文檔2、3、6、7和8,因為它們是成批的,並且“批次”沒有作廢; 之所以包含文件4和5,是因為盡管它們是分批的,但該批已作廢。 之所以包含文檔9,是因為它沒有作廢並且沒有分批處理
現在,假設我選擇了文檔4和5,並將它們添加到新批次中。
BatchID------IsVoid
1------------0-----
2------------1-----
3------------0-----
4------------0-----
BatchDocumentID-----BatchID------DocumentID
1-------------------1------------2---------
2-------------------1------------3---------
3-------------------2------------4---------
4-------------------2------------5---------
5-------------------3------------6---------
6-------------------3------------7---------
7-------------------3------------8---------
8-------------------4------------4---------
9-------------------4------------5---------
運行相同的查詢將返回相同的結果:
DocumentID
4
5
9
由於現在將文檔4和5關聯到非無效批次,因此我希望將它們從結果中排除,僅顯示文檔9。 顯然,這沒有發生。
如何重寫此查詢以顯示僅與無效批次相關聯的文檔,但如果它們也與非無效批次相關聯,則排除它們?
SELECT a.DocumentID
FROM tmpDocument a
LEFT JOIN tmpBatchDocument b ON a.DocumentID = b.DocumentID
LEFT JOIN tmpBatch c ON b.BatchID = c.BatchID
WHERE (
b.DocumentID IS NULL
AND a.IsVoid = 0 --WHY?
)
OR (c.IsVoid = 1
AND NOT EXISTS (select * from c2 where c2.batchID = (select batchID from b2 where b2.DocumentID=a.documentID) AND c2.isVoid=0)
)
編輯答案以包括“不存在”。 現在,您已經閱讀了完整的問題,這就是我想用來排除那些與文檔相關聯的非無效批次的記錄的條件。
使用COUNT
和HAVING
:
SELECT d.DocumentID
FROM tmpDocument d
LEFT JOIN tmpBatchDocument bd
ON bd.DocumentID = d.DocumentID
LEFT JOIN tmpBatch b
ON b.BatchID = bd.BatchID
WHERE
d.IsVoid = 0
GROUP BY d.DocumentID
HAVING
COUNT(bd.BatchDocumentID) = 0
OR COUNT(CASE WHEN b.IsVoid = 0 THEN 1 END) = 0
WHERE
子句顯然會排除無效的文檔。 HAVING
子句中的第一個條件將搜索未批處理的文檔,第二個條件將搜索已批處理但無效的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.