Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:59:25+00:00 2026-06-14T07:59:25+00:00

In a slight mental tangle and I expect this is easier than I imagine.

  • 0

In a slight mental tangle and I expect this is easier than I imagine.
Got the following tables:

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 

This returns the following:

enter image description here

I only want this query to return the rows when all of the three cardids exists for a handid. So the desired result set would not include handid 3.

This is a model of reality.
In reality #x contains 500 mill records.

EDIT

Ok – there are actually winners that are made up of sets of data from #winners which have a variable number of records. So amending the original code to the following the result set should not include handId 1 or handId 3. I am also getting some unwanted duplicate records in the result set:

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:59:26+00:00Added an answer on June 14, 2026 at 7:59 am

    You can use something like this:

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

    See SQL Fiddle with Demo

    Result:

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

    Based on your edit, here is an attempt that returns the correct result however I am not sure if it will work with the larger dataset that you have:

    ;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
    

    See SQL Fiddle with Demo

    Result:

    | HANDID |
    ----------
    |      2 |
    |      4 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a slight problem. I have set my <input type=file> to onChange=this.form.submit() .
(edit: Slight tidy of the code.) Using foreach like this works fine. var a
I ran into a slight coding problem with WordPress template. This is the code
I have a slight javascript problem, and this is the reason to why i
I have a slight dilema. This isn't general to backbone.js but it's certainly causing
I have a slight dilemma on my hands. This is the situation. On a
I am having a slight debate with a colleague of mine on this subject.
Got a slight problem trying to have jquery UI and knockout js to cohoperate.
This is a slight variance of this question: Possible to use a singleton with
I am having a slight performance issue with inline function. Consider the following code:-

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.