How do I use a select query to get the least of one value for each unique second value?
Question
There are groups like this;
USER_ID SEQ_ID NAME
1 2 Armut
1 3 Elma
1 4 Kiraz
2 1 Nar
2 2 Uzum
4 3 Sheftali
4 4 Karpuz
4 5 Kavun
After select query I want to see only;
USER_ID SEQ_ID NAME
1 2 Armut
2 1 Nar
4 3 Karpuz
That is, I want the row with the least SEQ_ID for each USER_ID. What SQL query will give me this result?
Best regards
Solution
Looks to me like it should be:
SELECT USER_ID, MIN(SEQ_ID) AS SEQ_ID, NAME
FROM table
GROUP BY USER_ID, NAME
ORDER BY USER_ID;
OTHER TIPS
SELECT USER_ID, SEQ_ID, NAME
FROM table
WHERE NAME IN ('Armut', 'Nar', 'Karpuz')
ORDER BY USER_ID
If you have something else in mind, please clarify your question.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow