4

在轻微的心理纠结中,我希望这比我想象的要容易。得到以下表格:

create table #x
(
handid int,
cardid int
)
insert into #x
values
(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),
(2,2),(2,3),(2,4),(2,300),(2,400),(2,500),(2,8),
(3,2),(3,3),(3,4),(3,300),(3,400),(3,7),(3,8),
(4,2),(4,300),(4,400),(4,500),(4,6),(4,7),(4,8)


create table #winners(cardid int)
insert into #winners values(300),(400),(500)

select a.* 
from 
        #x a 
        inner join #winners b
            on
            a.cardid = b.cardid 

这将返回以下内容:

在此处输入图像描述

我只希望此查询在 a 的所有三个cardids 都存在时返回行handid。因此,所需的结果集将不包括handid3。

这是现实的模型。实际上 #x 包含 500 条磨机记录。

编辑

好的 - 实际上有赢家是由#winners具有可变数量记录的数据集组成的。因此,将原始代码修改为以下结果集不应包含handId1 或handId3。我还在结果集中得到了一些不需要的重复记录:

create table #x
(
handid int,
cardid int
)
insert into #x
values
(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8000),
(2,2),(2,3),(2,4),(2,300),(2,400),(2,500),(2,8),
(3,2),(3,3),(3,4),(3,300),(3,400),(3,7),(3,8),
(4,2),(4,300),(4,400),(4,500),(4,6),(4,7),(4,8)


create table #winners(winningComb char(1), cardid int)
insert into #winners values('A',300),('A',400),('A',500),('B',8000),('B',400)

select a.* 
from 
        #x a 
        inner join #winners b
            on
            a.cardid = b.cardid 
4

4 回答 4

6

你可以使用这样的东西:

select handid
from #x  
where cardid in (select cardid from #winners)
group by handid
having count(handid) = (select count(distinct cardid)
                         from #winners);

请参阅带有演示的 SQL Fiddle

结果:

| HANDID |
----------
|      2 |
|      4 |

根据您的编辑,这是一个返回正确结果的尝试,但是我不确定它是否适用于您拥有的更大的数据集:

;with cte as
(   
    select w1.cardid, w1.winningComb, w2.ComboCardCount
    from winners w1
    inner join
    (
        select COUNT(*) ComboCardCount, winningComb
        from winners
        group by winningComb
    ) w2
        on w1.winningComb = w2.winningComb
) 
select a.handid
from x a
inner join cte b
    on a.cardid = b.cardid
where a.cardid in (select cardid from cte)
group by handid, b.ComboCardCount
having COUNT(a.handid) = b.ComboCardCount

请参阅带有演示的 SQL Fiddle

结果:

| HANDID |
----------
|      2 |
|      4 |
于 2012-11-14T11:52:04.810 回答
3

试试这个:

with cte as
(select a.* 
from 
        #x a 
        inner join #winners b
            on
            a.cardid = b.cardid ),
cte1 as 
     (select *,ROW_NUMBER() over(partition by handid order by cardid)  as row_num
       from cte),
cte2 as
     (select handid from cte1 where row_num=(select COUNT(*) from #winners) )
select * from cte where handid in (select handid from cte2)


SQL小提琴演示

于 2012-11-14T11:57:18.820 回答
1

您可以将查询更改为

select a.*, count(a.*) as a_count 
from 
    #x a 
    inner join #winners b
        on
        a.cardid = b.cardid 
group by a.handid 
having a_count = 3
于 2012-11-14T11:49:22.753 回答
1

@Bluefleets (+1) 方法在数据集的性能方面看起来不错;我猜有 5 亿条记录,性能概况将会改变。

我认为 OP 希望输出格式略有不同,稍微适应@Bluefleet 的代码会产生:

select * from #x where handid in (
select handid
from #x  
where cardid in (select cardid from #winners)
group by handid
having count(handid) = (select count(distinct cardid)
                         from #winners)
)
and cardid in (select cardid from #winners)

我还会考虑可怕的游标解决方案——因为它可能在大量记录上表现更好,具体取决于数据结构、索引、获胜者数量等。

但是如果没有完整的数据集,我真的不能说。

于 2012-11-14T12:00:34.210 回答