0

ALL. I have table looks like

NAME1 NAME2  Result
Jone  Jim     win
Kate  Lucy    loss
Jone  Lucy    win
Jim   Jone    loss

I want to select from NAME1 where win case>=3, My code is

SELECT NAME1,Count(Result='win') as WIN_CASE
From TABLE
Group by NAME1
Having Count(Result='win')>=3;

However, the result is not correct from the output, it just returns the total number of names shown in NAME1, what should I do to fix it please?

UPDATE: Thanks for all the reply. The result from Kritner and jbarker work fine. I just forget to add "where"Clause.

2
  • If one of the responses answered your question, you may consider marking this question as answered by selecting one of their responses. Commented Oct 13, 2014 at 13:11
  • Yeah, I chose one already. Thanks. Commented Oct 13, 2014 at 13:13

3 Answers 3

1

Query:

SELECT NAME1, COUNT(Result) AS WIN_CASE
FROM A
WHERE Result='win'
GROUP BY NAME1
HAVING COUNT(Result)>=3
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

select * 
from (select NAME1, Result, count(*) as res from test group by Result, NAME1) as t 
where t.res>=3 and t.Result ='win'

1 Comment

Please explain how this code helps the OP and further readers of this thred. You can find more about answering guidelines in the help center: How to Answer
0

You could do this:

http://sqlfiddle.com/#!6/02c47/1

select name1
from test
where result = 'win'
group by name1
having count(1) >= 3

Note that this isn't taking into account "Jone" wins when name 2 is jone and there's a loss (at least that's how i'm interpretting your shema.

So if name1 is jone, and it's a win, then jone won. But if jone is name2 and it's a loss, then jone won correct?

3 Comments

Thanks for the reply. I only want to select the names from NAME1 if and only if it is win and in NAME1. I don't consider jone in NAME2 and result is loss as it is win for jone.
May I ask why "count(1)"? why it is "1" not others?
No real reason, just quicker to write for me and produces same result. stackoverflow.com/questions/1221559/count-vs-count1

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.