cust_id | state |
---|---|
1 | CA |
2 | IL |
3 | SC |
1 | PA |
3 | IA |
4 | MO |
5 | VA |
4 | NY |
Can any one please advise on SQL Query that return the cust_id that belong to two states as below:
The output should be
cust_id | state |
---|---|
1 | CA |
1 | PA |
3 | SC |
3 | IA |
4 | NY |
4 | MO |
3
Answers
Try the following query
Solution 1
Solution 2
One simple approach would use a
COUNT
window function, that will assign the amount of times each "cust_id" occurs in your table. Once you get this value, you can filter out rows whose count is smaller than 2.Check the demo here.