SQL Query用于标识适用于不同数据集的记录

时间:2017-08-10 17:03:59

标签: sql sql-server tsql

我有以下查询返回查询下方显示的结果。但是,我只需要这些数据的一部分。我有兴趣获取与RegionID值2仅关联且与任何其他区域无关的DocumentID。

SELECT D.DocumentID, R.RegionID, COUNT(*) AS NUMOFPLANTSBYREGION 
FROM Document D INNER JOIN ShopAreaDoc SAD ON D.DocumentID = SAD.DocumentID 
INNER JOIN PlantShopAreaDoc PSAD ON SAD.ShopAreaDocID = PSAD.ShopAreaDocID 
INNER JOIN Plant P ON PSAD.PlantID = P.PlantID 
INNER JOIN Region R ON P.RegionID = R.RegionID 
GROUP BY D.DocumentID, R.RegionID 
ORDER BY D.DocumentID 

查询结果:

+------------+----------+---------------------+
| DocumentID | RegionID | NUMOFPLANTSBYREGION |
+------------+----------+---------------------+
|       2126 |        2 |                   8 |
|       2127 |        2 |                   8 |
|       2128 |        2 |                   8 |
|       2129 |        2 |                   8 |
|       2130 |        2 |                   8 |
|       2134 |        4 |                  13 |
|       2135 |        3 |                   8 |
|       2136 |        6 |                   9 |
|       2137 |        2 |                   8 |
|       2138 |        3 |                   8 |
|       2138 |        1 |                  20 |
|       2138 |        6 |                   9 |
|       2138 |        4 |                  14 |
|       2138 |        2 |                   8 |
|       2139 |        1 |                  17 |
|       2140 |        1 |                  17 |
+------------+----------+---------------------+

我感兴趣的结果集如下:

其他记录不适用于区域ID为2或适用于除2以外的更多区域,因此应予以排除。

+------------+----------+----------------------+
| DocumentID | RegionID | NUMOFPLANTSBYREGION  |
+------------+----------+----------------------+
|       2126 |        2 |                    8 |
|       2127 |        2 |                    8 |
|       2128 |        2 |                    8 |
|       2129 |        2 |                    8 |
|       2130 |        2 |                    8 |
|       2137 |        2 |                    8 |
+------------+----------+----------------------+

3 个答案:

答案 0 :(得分:2)

使用common table expression;with cte as ( SELECT D.DocumentID, R.RegionID, COUNT(*) AS NUMOFPLANTSBYREGION FROM Document D INNER JOIN ShopAreaDoc SAD ON D.DocumentID = SAD.DocumentID INNER JOIN PlantShopAreaDoc PSAD ON SAD.ShopAreaDocID = PSAD.ShopAreaDocID INNER JOIN Plant P ON PSAD.PlantID = P.PlantID INNER JOIN Region R ON P.RegionID = R.RegionID GROUP BY D.DocumentID, R.RegionID ) select * from cte where not exists ( select 1 from cte i where i.DocumentID = cte.DocumentID and i.RegionID <> 2 )

+------------+----------+----------------------+
| DocumentID | RegionID | NUMOFPLANTSBYREGION  |
+------------+----------+----------------------+
|       2126 |        2 |                    8 |
|       2127 |        2 |                    8 |
|       2128 |        2 |                    8 |
|       2129 |        2 |                    8 |
|       2130 |        2 |                    8 |
|       2137 |        2 |                    8 |
+------------+----------+----------------------+

rextester演示:http://rextester.com/DUIE27467

返回:

{{1}}

答案 1 :(得分:1)

  SELECT D.DocumentID, R.RegionID, COUNT(*) AS NUMOFPLANTSBYREGION 
    FROM Document D 
    INNER JOIN ShopAreaDoc SAD ON D.DocumentID = SAD.DocumentID 
    INNER JOIN PlantShopAreaDoc PSAD ON SAD.ShopAreaDocID = PSAD.ShopAreaDocID 
    INNER JOIN Plant P ON PSAD.PlantID = P.PlantID 
    INNER JOIN Region R ON P.RegionID = R.RegionID 
    WHERE R.RegionId = 2
    AND NOT EXISTS (SELECT * FROM Region R2 WHERE R2.RegionId <> 2 AND R2.DocumentId = D.documentid)
    GROUP BY D.DocumentID, R.RegionID 
    ORDER BY D.DocumentID 

答案 2 :(得分:-1)

你可以使用这种方法,实现MINUS语句(顺便说一下,MySQL中不存在):

SELECT DocumentID FROM docs WHERE RegionID=2 AND DocumentID NOT IN (SELECT DocumentID FROM docs WHERE RegionID<>2)

我也在http://sqlfiddle.com/#!9/66f0e8/57上尝试了它,它似乎有效。