0

I have a table like this:

ID | AGE 
--------
 1 | 27
 2 | 24
 1 | 25
 4 | 25
 1 | 25
 5 | 25
 5 | 25
...|...

As you can see, ID and AGE are not unique (there is a second ID that is unique, but that's irrelevant here).

I want to return all the distinct IDs with more than one specific value: for example, counting all the IDs with more than one AGE = 25; in the table above, this should yield [1, 5], (ID = 4 is not counted because 25 occurs only once; ID = 1 and ID = 5 IS included because 25 occurs more than once in AGE).

Basically what I want in invalid SQL:

SELECT DISTINCT id FROM table WHERE count(AGE = 25) > 1

Unfortunately, count(AGE = 25) is invalid. How would I write this out in SQL?

1
  • You can simply do this in SQL Commented Nov 20, 2013 at 8:45

1 Answer 1

3

Do you want something like this?

SELECT 
 ID, COUNT(ID)
FROM table
WHERE Age = 25
GROUP BY ID
HAVING COUNT(ID) > 1
Sign up to request clarification or add additional context in comments.

2 Comments

It it possible to count, foreach distinct ID, the number of rows where AGE = 25?
I changed my answer to what, i think, you want.

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.