select only where a result exists in all joins?

Hopefully this shows what I am trying to do... I need to join the list of groups in the Tblsearch to the groups in the tblData table but return a row only where the is matched on all other joins.
My approach to date has been to select for each row in the tblsearch the items from tblData with matching groupID and then inner join all the results but this somewhat messy to construct and not that good performance.   My real world data typically has only 5-10 rows (values in the search but up to about a million rows in the data table).
 allgroups.pngAllGroups.xls
dgloverukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
This result:
| tbl | item |
|-----|------|
|  t2 |    1 |
|  t1 |    3 |
|  t2 |    3 |

Open in new window


ws produced by this query:
select
   s.tbl, d.item
from tbldata d
inner join (
  select GroupID, 't1' as tbl, count(*) over(partition by 1) as tblcount from tblSearch1
  union
  select GroupID, 't2' as tbl, count(*) over(partition by 1) as tblcount from tblSearch2
   ) s on d.GroupID = s.GroupID
group by
   s.tbl, d.item
having    
   count(*) = max(s.tblcount)

Open in new window


details:
    CREATE TABLE tblData
        ([GroupID] int, [Item] int) 
    ;
        
    INSERT INTO tblData
        ([GroupID], [Item])
    VALUES
        (1, 1),
        (1, 2),
        (1, 3),
        (2, 3),
        (2, 4),
        (2, 5),
        (3, 1),
        (3, 3),
        (4, 1),
        (4, 3),
        (5, 1),
        (5, 3),
        (6, 1),
        (6, 2),
        (7, 2),
        (7, 4)
    ;
    
    
    CREATE TABLE tblSearch1
        ([GroupID] int)
    ;
        
    INSERT INTO tblSearch1
        ([GroupID])
    VALUES
        (1),
        (2),
        (3)
    ;
    
    
    CREATE TABLE tblSearch2
        ([GroupID] int)
    ;
        
    INSERT INTO tblSearch2
        ([GroupID])
    VALUES
        (4),
        (5)
    ;
    
**Query 1**:

    select
       s.tbl, d.item
    from tbldata d
    inner join (
      select GroupID, 't1' as tbl, count(*) over(partition by 1) as tblcount from tblSearch1
      union
      select GroupID, 't2' as tbl, count(*) over(partition by 1) as tblcount from tblSearch2
       ) s on d.GroupID = s.GroupID
    group by
       s.tbl, d.item
    having    
       count(*) = max(s.tblcount)

**[Results][2]**:
    | tbl | item |
    |-----|------|
    |  t2 |    1 |
    |  t1 |    3 |
    |  t2 |    3 |

  [1]: http://sqlfiddle.com/#!3/a0ff0/1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgloverukAuthor Commented:
Hi Paul, this is nearly exactly what I wanted, I think I misled you to go too far with the solution.
I provided 2 examples in my sheet but I was actually only interested in a one at a time scenario , they didn't need unionizing, I was just trying to give 2 different examples.
Would you be so kind as to simplify your solution so it would work with a given tblSearch and a tblData.

Thank you so much Paul.
0
dgloverukAuthor Commented:
I think solved this :
	select d.item from tbldata d 
	inner join(select Item, count(*) over(partition by 1) as tblcount from tblSearch2 s 
	on d.GroupID = s.Item
    group by
       d.item
    having    
       count(*) = max(s.tblcount)

Open in new window

That's about as efficient as I can get it isn't it Paul?
0
PortletPaulfreelancerCommented:
Yes, I believe it is.
0
dgloverukAuthor Commented:
More than I needed, thanks Paul!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.