Find all sets/entities that are in another set [duplicate]

空扰寡人 提交于 2019-12-23 06:14:16

问题


The answer is found in the abstract here but I'm looking for the concrete SQL solution.

Given the following tables:

   ------------     -----------
   |  F_Roles |     | T_Roles | 
   ------+-----     -----+-----
   | FId | RId|     |TId | RId|
   ------+------    -----+-----
   |  f1 |  2 |     | t1 | 1  |
   |  f1 |  3 |     | t1 | 2  |
   |  f2 |  2 |     | t1 | 3  |
   |  f2 |  4 |     | t1 | 4  |
   |  f2 |  9 |     | t1 | 5  |
   |  f3 |  6 |     | t1 | 6  |
   |  f3 |  7 |     | t1 | 7  |
   ------------     ----------

(F_Roles) is a join table between F (not shown) and Roles (also not shown) (T_Roles) is a join table between T (not shown) and Roles (not shown)

I need to return:

  1. all (DISTINCT) FId's where the set of RId's for a given FId is a subset of (or 'IN') Roles. (I know I'm mixing Set Theory with database terms but only in the interest of better conveying the idea, I hope). So, f1 and f3 should be returned in this case, because the set of RId's for f1, {2,3}, and for f3, {6,7}, are subsets of T_Roles.

  2. the list of RId's in T_Roles NOT found in any of the functions returned above. (T_Roles - (f1 Union f3)), or {1,4,5} in this example.


回答1:


Let's define the following sample data:

DECLARE @F_Roles TABLE
(
    [FID] CHAR(2)
   ,[RID] TINYINT
);

DECLARE @Roles TABLE
(
    [RID] TINYINT
);

INSERT INTO @F_Roles ([FID], [RID])
VALUES ('f1', 2)
      ,('f1', 3)
      ,('f2', 2)
      ,('f2', 4)
      ,('f2', 9)
      ,('f3', 6)
      ,('f3', 7);

INSERT INTO @Roles ([RID])
VALUES (1), (2), (3), (4), (5), (6), (7);

No, the first query can be solved using the T-SQL statement below:

SELECT F.[FID] 
FROM @F_Roles F
LEFT JOIN @Roles R
    ON F.[RID] = R.[RID]
GROUP BY F.[FID]
HAVING SUM(CASE WHEN R.[RID] IS NULL THEN 0 ELSE 1 END) = COUNT(F.[RID]);

The idea is pretty simple. We are using LEFT join in order to check which RID from the @F_Roles table has corresponding RID in the @Rolestable. If it has not, the value returned by the query for the corresponding row is NULL. So, we just need to count the RIDs for each FID and to check if this count is equal to the count of values returned by the second table (NULL values are ignored).

The latter query is simple, too. Having the FID from the first, we just can use EXCEPT in order to found RIDs which are not matched:

SELECT [RID]
FROM @Roles
EXCEPT
SELECT [RID]
FROM @F_Roles
WHERE [FID] IN
(
    SELECT F.[FID] 
    FROM @F_Roles F
    LEFT JOIN @Roles R
        ON F.[RID] = R.[RID]
    GROUP BY F.[FID]
    HAVING SUM(CASE WHEN R.[RID] IS NULL THEN 0 ELSE 1 END) = COUNT(F.[RID])
);

Here is the result of the execution of the queries:




回答2:


For query 1:

with x as (
select f.fid, sum(case when r.rid is null then 1 end) as missing
  from f_roles f
  left join roles r on r.rid = r.rid
  group by f.fid
)
select distinct f.fid
  from f_roles f
  join x on f.fid = x.fid
  where x.missing = 0

For query 2:

with x as (
select f.fid, sum(case when r.rid is null then 1 end) as missing
  from f_roles f
  left join roles r on r.rid = r.rid
  group by f.fid
),
y as (
select distinct f.fid
  from f_roles f
  join x on f.fid = x.fid
  where x.missing = 0
)
select r.rid
  from roles r 
  where r.rid not in (
    select f.rid from y join f_roles f on f.rid = y.rid
  )


来源:https://stackoverflow.com/questions/53051756/find-all-sets-entities-that-are-in-another-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!