Sunday, February 19, 2012

Help me getting a unique combination from table

Hi, I have following table

BetId GameId
==== ======
500 108
500 109
501 108
501 109
501 110
502 108
502 109

My query would have the form: select BetId where GameId in(108,109)
from Bets then it has to get me BetId : 500 and 502.
Not 501,since this is different combination(108,109,110) ;)drop table test
create table test(BetId int,GameId int)
go
insert test values(500,108)
insert test values(501,109)
insert test values(501,108)
insert test values(501,110)
insert test values(502,108)
insert test values(502,109)
go
select BetId
from test
where GameId in(108,109)
group by BetId having count(*)=2|||Hi there.

think this would do it (if you know how many GameIDs you expect):

select b.BetID
from Bets b
,(select distinct BetID
from Bets
where GameID in (108, 109) ) as a
where b.BetID = a.BetID
group by b.BetID
having count(*) = 2

Greetings,
Carsten|||So what you want is a list of betId values that are used in a set of gameId values, but only in that set of gameId values, right? IF I understand that correctly, then I'd use:SELECT a.betId
FROM Bets AS a
WHERE 2 = (SELECT Count(*)
FROM Bets AS b
WHERE b.betId = a.betId
AND b.gameId IN (108, 109))
GROUP BY a.betId
HAVING 2 = Count(*)-PatP

No comments:

Post a Comment