mysql - Distinct with a group by -


i need write query filter data. in table have data grouped val1 multiple values of val2 witch have remove.

my table this:

| id | val1 | val2 | other | |------------------|------- | 1  | a1   |  b1  | ... | 2  | a1   |  b1  | ... | 3  | a1   |  b2  |  | 4  | a2   |  b1  |  | 5  | a3   |  b1  | | 6  | a3   |  b1  | | 7  | a3   |  b2  |  | 8  | a4   |  b1  | | 9  | a4   |  b3  | | 10 | a5   |  b1  | 

and need this:

| id | val1 | val2 | |------------------| | 1  | a1   |  b1  | | 3  | a1   |  b2  | | 4  | a2   |  b1  | | 5  | a3   |  b1  | | 7  | a3   |  b2  | | 8   ... | 9   ... | 10  ... 

it's sort of select *,distinct(val2) table group val1..

you on right track using group by

following should return results per question

select min(id) id, val1, val2   yourtable group        val1, val2 

breakdown

  • use aggregate function on column don't wish group on. in our example min aggregate function on id column. returns lowest id each group
  • group by columns want distinct value of

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -