Getting max record


This short article was written for group by and max usage.  If you need querying a table which items could have more than one values by max date, you can follow these steps.

First of all lets look at our table :


As you can see the table has more than one record for each id.  If you want get the values which have max date you should use following query and join.

select id,MAX(date) dt from tabletest 
group by id


This query shows us records which have max date.

select t1.* from tabletest t1
inner join (
select id,MAX(date) dt from tabletest
group by id
) t2 on t1.id = t2.id and t1.date = t2.dt



and this query shows us the values which have max date


We used first query in the second query as a table and joined two tables via id and date columns.

Comments

Popular posts from this blog

XML parsing unable to switch the encoding

d3.js Introduction

HTML Vector Graphic PATH element