在轻微的心理纠结中,我希望这比我想象的容易。 得到以下表格:
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
返回以下内容:
我只希望此查询在cardid
存在三个handid
的所有时返回行。因此,期望的结果集不包括handid
3。
这是现实的典范。 实际上,#x包含500个记录。
修改
好的 - 实际上获胜者由来自#winners
的数据集组成,这些数据集具有可变数量的记录。因此,将原始代码修改为以下结果集不应包括handId
1或handId
3.我还在结果集中获得了一些不需要的重复记录:
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
答案 0 :(得分:6)
您可以使用以下内容:
select handid
from #x
where cardid in (select cardid from #winners)
group by handid
having count(handid) = (select count(distinct cardid)
from #winners);
结果:
| 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
结果:
| HANDID |
----------
| 2 |
| 4 |
答案 1 :(得分: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)
答案 2 :(得分: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
答案 3 :(得分: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)
我还会考虑恐惧游标解决方案 - 因为它可能会在大量记录上表现更好,具体取决于数据结构,索引,获胜者数量等。
但是没有完整的数据集,我真的不能说。