how to use SQL group to filter rows with maximum date value -


i have following table

create table test     (`id` int, `value` varchar(20), `adate` varchar(20)) ;  insert test     (`id`, `value`, `adate`) values     (1, 100, '2014-01-01'),     (1, 200, '2014-01-02'),     (1, 300, '2014-01-03'),     (2, 200, '2014-01-01'),     (2, 400, '2014-01-02'),     (2, 30 , '2014-01-04'),     (3, 800, '2014-01-01'),     (3, 300, '2014-01-02'),     (3, 60 , '2014-01-04') ; 

i want achieve result selects id having max value of date. ie

id ,value ,adate

 1, 300,'2014-01-03'       2, 30 ,'2014-01-04'       3, 60 ,'2014-01-04' 

how can achieve using group by? have done follows not working.

select id,value,adate test group id,value,adate having adate = max(adate) 

can query?

if using dbms has analytical functions can use row_number:

select  id, value, adate    (   select  id,                     value,                     adate,                     row_number() over(partition id order adate desc) rownum                test         ) t   rownum = 1; 

otherwise need use join aggregated max date id filter results test date matches maximum date id

select  test.id, test.value, test.adate    test         inner join         (   select  id, max(adate) adate                test             group id         ) maxt             on maxt.id = test.id             , maxt.adate = test.adate; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -