views:

121

answers:

2

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

+9  A: 
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.

outis
Nice. That's Occam's razor right there.
Manu
Except I had it wrong at first. Forgot the `ORDER BY` clause.
outis
+4  A: 

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;
Greg Low
That doesn't work for the group with USER_ID 4. The smallest SEQ_ID would be 3 there.
Joey
@Greg: powertip--indent text with four spaces to have it formatted as code. Clicking the orange question mark you see in the toolbar when posting takes you to a doc describing this and other formatting.
outis
For reference, here's the doc: http://stackoverflow.com/editing-help
outis
thanks Greg for the help.It was my mistake, that must be 3 instead of 4.Thanks for your understanding my question.best regardsps:it would be better asking "if you mean 3" instead of judging me with minus vote...
blgnklc
blgnklc: It wasn't obvious from your question what exactly you wanted. Basically there is an arbitrary number of possible options. And there it an arbitrary number of possible options if you include minor mistakes or typos. And a downvote means "This question is unclear or not useful" which, for a clearly unclear question, seems very much to be the case.
Joey
thank you Johannes you keep me "AS UNUSEFUL".
blgnklc
@blgnklc: when you get downvoted for an unclear question, you can edit the question and clarify. You'll then get upvoted. That's just how SO works.
outis
@outis: I have cleared the question so why I am still minus 2? </br> never mind.. </br> best regards –
blgnklc
@blgnklc: "this result" is still ambiguous. My answer, for instance, is still a valid answer for your question, though it shouldn't be. State that you want the row with the least SEQ_ID for every USER_ID, if that's a fair description.
outis
@outis: I want the row with the least SEQ_ID for every USER_ID.
blgnklc