0

My table have the following kind of entries:

Serial Number | Cycle Number

55                33

56                27

55                34

55                33

57                5

57                6

56                28

57                6

I would like to return distinct Serial Numbers that have multiple Cycle Numbers with the same value

Serial Number |  Cycle Number

55                33

55                33

57                6

57                6 

Any help would be appreciated.

1
  • Why do you need four rows? Wouldn't 2 rows with a count provide the same information? Commented Jan 28, 2019 at 20:17

3 Answers 3

2

You can use the following query that will return the distinct Serial Number which have the multiple Cycle Number with same value.

SELECT SerialNumber, CycleNumber
FROM Table
GROUP BY SerialNumber, CycleNumber HAVING COUNT(SerialNumber)>1
Sign up to request clarification or add additional context in comments.

Comments

1

If you really only have two columns, you could do:

select serial, cycle
from t
where (serial, cycle) in (select serial, cycle
                          from t t2
                          group by serial, cycle
                          having count(*) >= 2
                         );

But why wouldn't you just do this?

select serial, cycle, count(*)
from t
group by serial, cycle;

Comments

0

SQL Syntax:

SELECT A.* FROM tmpTable A
LEFT JOIN
( 
SELECT SerialNumber,CycleNumber FROM tmpTable  GROUP BY  SerialNumber, CycleNumber having count(SerialNumber)>1
) B ON A.SerialNumber = B.SerialNumber And A.CycleNumber = B.CycleNumber
WHERE B.SerialNumber is not null

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.