1

I have two tables called game and gameprogress. Gameprogress has a column called state that describes progress. Game is related to gameprogress trough an id.

Possible states is: 1,3,4 in gameprogress.

I want to find games without a state = 1 and without a state = 4. I've tried something like this:

select top 10 gp.gameid, count(gp.state) as dup
from gameprogress gp 
join game g on g.id= gp.gameid
where g.gamestate != 2 and gp.state != 1
group by gp.gameid
having count(gp.state)>1

1 Answer 1

1

You can use CASE EXPRESSION in the HAVING clause :

SELECT TOP 10 g.gameid
FROM game g
LEFT JOIN gameprogress gp 
 ON g.id= gp.gameid
GROUP BY g.gameid
HAVING COUNT(CASE WHEN gp.state = 3 THEN 1 END) = COUNT(*)
Sign up to request clarification or add additional context in comments.

6 Comments

Looks like the right way to go. But i have an issue in not getting the games with state = 4, i've tried to:SELECT g.id FROM game g LEFT JOIN gameprogress gp ON g.id= gp.gameid where g.gamestate != 2 and gp.state != 4 GROUP BY g.id HAVING COUNT(CASE WHEN gp.state = 1 THEN 1 END) = 0
Why should it return games with state = 4? Should I guess the data? Edit the question and provide the data sample!
My fault. Edited the question.
@farnholdt So you want only those who has state = 4 or state = 2 ? Explain why so I'll know which filters to add
I only want those games which only has state = 3 and therefor not has any other states.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.