sql - Order by with distinct -
firstly sql:
select distinct(cartable.carplate), cartable.carimage, eventtable.firstdate cartable join eventtable on cartable.carid = eventtable.carid order 3 desc
i trying order rows date , works fine want plates written 1 time. mean need see car's last event seems using distinct wrong. best way of doing ?
use max
, group by
:
select c.carplate, c.carimage, firstdate = max(e.firstdate) cartable c inner join eventtable e on c.carid = e.carid group c.carplate, c.carimage order max(e.firstdate) desc
note:
- use meaningful alias improve readability.
Comments
Post a Comment