0

I am very new to MySQL (I tried groupby, but didnt get the result) This my sample data

ticket year status data
111    2007 1      32
111    2007 2      46
111    2007 3      40
111    2007 4      38
111    2009 1      15
111    2009 2      12
115    2009 1      2 
115    2009 2      5

I want to select the last entry for each company, for each year. Output should be:

ticket year status data
111    2007 4      38 
111    2009 2      12
115    2009 2      5
Henil Shah
  • 137
  • 4
  • 14
  • You should be able to get it with a group by , can you show what you did? – P.Salmon Aug 16 '17 at 08:03
  • You **cannot** select rows using `GROUP BY`. `GROUP BY` computes new rows using the data from each group. Take a look at [this answer](https://stackoverflow.com/a/28090544/4265352) on a [similar question](https://stackoverflow.com/q/12102200/4265352). – axiac Aug 16 '17 at 08:15

1 Answers1

0

as status field values looks incremental order. we can simply apply max after groupby.

select a.* from a 
inner join (select ticket,year,max(status) as sta from a group by ticket,year) as b 
on a.status=b.sta 
and a.ticket=b.ticket and a.year=b.year;
  • Updated the question. This is what I had tried. I need to select the row which is corresponding to max(status) – Henil Shah Aug 16 '17 at 08:23