SQL Server table add id of last visits in GROUP query -


i have sql server database table list of users , last visit. extract list of last visit visitors, table id.

let me explain better giving example. here's simplified version

visits | id | visitor | last_visit | |----|---------|------------| |   1|     abc | 2014-04-06 | |   2|     def | 2014-04-06 | |   3|     ghi | 2014-04-07 | |   4|     def | 2014-04-07 | |   5|     abc | 2014-04-08 | |   6|     jkl | 2014-04-10 | |   7|     def | 2014-04-12 | 

and here's obtain:

| id | visitor | last_visit | |----|---------|------------| |   3|     ghi | 2014-04-07 | |   5|     abc | 2014-04-08 | |   6|     jkl | 2014-04-10 | |   7|     def | 2014-04-12 | 

i managed result without id column doing simple group:

select visitor, max(last_visit) visits group visitor 

but don't have clue on how add id, fundamental purpose.

try this:

select a.id, a.visitor, a.last_visit visits  inner join (select visitor, max(last_visit) last_visit              visits              group visitor           ) b on a.visitor = b .visitor , a.last_visit = b.last_visit; 

or

select a.id, a.visitor, a.last_visit  (select row_number() on (partition a.visitor order a.last_visit desc) rowno,               a.id, a.visitor, a.last_visit       visits     )  rowno = 1; 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -