1

This is the table1 structure:

ID_Num | id_2 | field1
12-001 | 3    | ABCD
12-001 | 3    | ABCD
12-001 | 3    | ABCD
12-001 | 1    | ABCD
12-001 | 1    | ABCD
12-002 | 3    | ABCD
12-002 | 3    | ABCD
12-002 | 3    | ABCD
12-002 | 3    | ABCD
12-002 | 3    | ABCD
12-003 | 2    | ABCD
12-003 | 2    | ABCD
12-003 | 3    | ABCD
12-003 | 3    | ABCD
12-003 | 3    | ABCD

Now I want to select all ID_Num that has a change in field id_2
It should return 12-001 because of change from 3 to 1
and 12-003 because of change from 2 to 3

2 Answers 2

3
SELECT  ID_NUM
FROM    tableNAME
GROUP   BY ID_NUM
HAVING  COUNT(DISTINCT id_2) > 1
Sign up to request clarification or add additional context in comments.

2 Comments

@paynet: Careful about relying on MySQL's GROUP BY/hidden column functionality
@OMGPonies: This query doesn't though, does it?
0

I realize that count(distinct) requires more processing than the following:

select id_num
from tablename
group by id_num
having min(id_2) <> max(id_2)

This does not treat NULL as a separate value, but there are no NULLs in the question.

Comments

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.