Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集合包含SQL中的其他集合_Sql_Sql Server - Fatal编程技术网

集合包含SQL中的其他集合

集合包含SQL中的其他集合,sql,sql-server,Sql,Sql Server,有人知道如何找到包含所有其他集合的集合吗 桌子 我想得到这个: id 2 3 在这种情况下,ID2包含一个,C-ID1的所有集合 而id 3包含F-所有id 4的集合 在我的查询中,我需要获取包含所有set-all元素(至少一个id)的all-id。 我将非常感激。谢谢。这一次我没有太多东西可供参考,但它很感兴趣,所以我猜。我正在使用SQL Server 我提交了包含嵌套选择的答案,然后我按照一步一步的逻辑来澄清发生了什么: 建立一个快速表 CREATE TABLE a (id int, t v

有人知道如何找到包含所有其他集合的集合吗

桌子

我想得到这个:

id 2 3 在这种情况下,ID2包含一个,C-ID1的所有集合 而id 3包含F-所有id 4的集合

在我的查询中,我需要获取包含所有set-all元素(至少一个id)的all-id。
我将非常感激。谢谢。

这一次我没有太多东西可供参考,但它很感兴趣,所以我猜。我正在使用SQL Server

我提交了包含嵌套选择的答案,然后我按照一步一步的逻辑来澄清发生了什么:

建立一个快速表

CREATE TABLE a (id int, t varchar(10))
INSERT INTO a (id, t)
VALUES (1,'A'),(1,'B'),(1,'B'),(1,'B'),(2,'A')
,(2,'B'),(2,'C'),(3,'C'),(3,'D'),(4,'A'),(5,'P');
以下是解决方案:

select distinct p_id supersets from 
(
    select e.p_id, e.c_id, count(*) matches from (
        select distinct c.id p_id, c.t p_t, d.id c_id, d.t c_t from a c
        inner join a d on c.t = d.t and c.id <> d.id) e
    group by e.p_id, e.c_id) sup
inner join
    (select id, count(distinct t) reqs from a group by id) sub
on sub.id = sup.c_id and sup.matches = sub.reqs;
以下是逻辑步骤,有助于解释我为什么要这么做:

--Step1 
--Create a list of matches between (distinct) values where IDs are not the same
select distinct c.id p_id, c.t p_t, d.id c_id, d.t c_t from a c
inner join a d on c.t = d.t and c.id <> d.id;

--Step2
--Create a unique list of parent IDs and their child IDs and the # of distinct matches
--For example 2 has 2 matches with 1 and vice versa
select e.p_id, e.c_id, count(*) matches from (
        select distinct c.id p_id, c.t p_t, d.id c_id, d.t c_t from a c
        inner join a d on c.t = d.t and c.id <> d.id) e
group by e.p_id, e.c_id;

--Step2a
--Create a sub query to see how many distinct values are in each "Set"
select id, count(distinct t) reqs from a group by id;

现在我们将其全部放在上面的联接中,首先要确保父级到子级的匹配总数占子级值的100%是一个超集

我认为以下内容符合您的要求:

select t1.id
from t t1 join
     (select t.*, count(*) over (partition by id) as cnt
      from t
     ) t2
     on t1.elem = t2.elem and t1.id <> t2.id
group by t1.id, t2.id, t2.cnt
having count(*) = cnt;
这会根据元素将每个id与另一个id相匹配。如果匹配的数量等于第二个集合中的计数,则所有匹配-并且您有一个超集

我注意到您有重复的元素。让我们用CTE来处理这个问题:

with t as (
      select distinct id, elem
      from t
     )
select t1.id
from t t1 join
     (select t.*, count(*) over (partition by id) as cnt
      from t
     ) t2
     on t1.elem = t2.elem and t1.id <> t2.id
group by t1.id, t2.id, t2.cnt
having count(*) = cnt;

您当然可以尝试一下?您使用的是哪种sql?什么是rdbms ie mysql、oracle等谢谢,请尽可能详细易懂
with t as (
      select distinct id, elem
      from t
     )
select t1.id
from t t1 join
     (select t.*, count(*) over (partition by id) as cnt
      from t
     ) t2
     on t1.elem = t2.elem and t1.id <> t2.id
group by t1.id, t2.id, t2.cnt
having count(*) = cnt;