sql - Returning a list of rows that are unique by type and returning the first pass -


id userid type    pass   date  1  12 track1  1         20140101 2  32 track2  0     20140105 3  43 pull1   1     20140105 4  66 pull2   1     20140110 5  54 pull1   0     20140119 6  54 track1  0     20140120 

so users can take multiple attempts 'type', can take 'track1' multiple times, or 'pull2' multiple times.

i want return first pass (1) each unique 'type' each user.

i want return both pass , fail rows, first instance of pass or fail.

how can this?

sample table , output

id userid type    pass   date  1  12 track1  1     20140101 2  12 track2  0     20140105 3  12 pull1   1     20140105 4  12 pull2   1     20140110 5  12 pull1   0     20140119 6  12 track1  0     20140120 7  12 track1  0     20140121 8  12 pull1   1     20140115 9  12 track2  0     20140125  output: 1  12 track1  1     20140101 2  12 track2  0     20140105 3  12 pull1   1     20140105 4  12 pull2   1     20140110 

select t1.* usertrackstatus t1 join (   select userid,           type,           min(date) min_date     usertrackstatus     group userid, type ) t2 on t1.userid = t2.userid , t1.type = t2.type , t1.date = t2.min_date 

sqlfiddle


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -