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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -