0

I have a table as below :


col1         col2        col3      col4
---------------------------------------
B1           01          ABC        001
B1           02          ABC        001
B1           03          ABC        001
B1           04          ABC        002
B1           05          ABC        002
B1           06          ABC        003
B1           07          ABC        003
B1           08          ABC        004
B1           09          ABC        004
B1           01          DEF        001
B1           02          DEF        001
B1           03          DEF        002
B1           04          DEF        002
B1           05          DEF        002
B1           06          DEF        003
B1           07          DEF        004
B1           08          DEF        004
B1           09          DEF        004

What I would like to have is a filtered table. for every distinct col3 value, it should have distinct elements from col4 and those elements must be chosen by col2 should be the minimum value of the col2. Let me express what I want to have:

 col1         col2        col3      col4
---------------------------------------
B1           01          ABC        001
B1           04          ABC        002
B1           06          ABC        003
B1           08          ABC        004
B1           01          DEF        001
B1           03          DEF        002
B1           06          DEF        003
B1           07          DEF        004

Is there any way to implement this in SQL? By the way, I am using SQLite. I do not want to write a Java code to iterate every row of the query results.

2
  • 1
    Good first question but what have you tried so far? Sep 1, 2014 at 2:18
  • I've tried tons of sql statement, but no lack. Sep 1, 2014 at 2:28

1 Answer 1

1
select col1, min(col2), col3, col4
from mytable
group by col1, col3, col4
order by col1, col3, col4
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.