| Author |
Topic |
|
Lambik
Starting Member
13 Posts |
Posted - 2010-05-06 : 15:21:20
|
| Hello,I have the following table:name date locationJohn 20100202 ParisJohn 20100204 BerlinJohn 20100206 AmsterdamEric 20100204 LondonEric 20100209 Copenhagenwhen the output is:John 2010206Eric 2010209the sql could be something likeselect name, max(date)from tablenamegroup by namewhen the output is:John 2010206 AmsterdamEric 2010209 Copenhagenhow should the the sql be?select name, max(date), location -- this is not workingfrom tablenamegroup by namean aggregation error is givenwhen somebody has a good idea please helpLambik |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-05-06 : 15:54:51
|
| [code]select a.name,a.date,b.locationfrom(select name, max(date) as datefrom tablenamegroup by name) ainner join tablename b on a.name = b.name and a.date = b.date[/code] |
 |
|
|
Lambik
Starting Member
13 Posts |
Posted - 2010-05-07 : 02:52:14
|
| Thanx vijayisonly your query is working greatgreet Lambik |
 |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2010-05-07 : 03:09:06
|
| CREATE TABLE #TMPS( name varchar(100), dates datetime, location varchar(100))insert into #TMPSselect 'John','20100202','Paris'unionselect 'John','20100204','Berlin'unionselect 'John','20100206','Amsterdam'unionselect 'Eric','20100204','London'unionselect 'Eric','20100209','Copenhagen'select t.* from( select *,[rowno]=ROW_NUMBER() over (partition by name order by dates desc) from #TMPS ) twhere t.rowno =1order by t.nameIam a slow walker but i never walk back |
 |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2010-05-07 : 03:31:54
|
quote: Originally posted by vijayisonly
select a.name,a.date,b.locationfrom(select name, max(date) as datefrom tablenamegroup by name) ainner join tablename b on a.name = b.name and a.date = b.date
Hi yours will work fine but what happens when the same person has more than 1 record with same date. then u will get 2 records for john then one.thanksIam a slow walker but i never walk back |
 |
|
|
Lambik
Starting Member
13 Posts |
Posted - 2010-05-07 : 04:34:29
|
| DineshajanI have tried it with more than 1 record with same date :the first solution will indeed return 2 rows. Your solution return one row. In our company I will see both the visits. (2 visits on the same date time is not possible however)your sql syntax is new for me Dineshajan (select *,[rowno]=ROW_NUMBER() over (partition by name order by dates desc)I will read something about it thanx anywayLambik |
 |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2010-05-07 : 05:01:21
|
| Hi,What u said is correct. U can try this. this will help urs.select t.* from( select *,[rowno]=dense_rank() over (partition by name order by dates desc) from #TMPS ) twhere t.rowno =1Iam a slow walker but i never walk back |
 |
|
|
Lambik
Starting Member
13 Posts |
Posted - 2010-05-07 : 05:04:44
|
| Dineshrajanyes that will return all the rows thxLambik |
 |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2010-05-07 : 05:07:28
|
Welcome Iam a slow walker but i never walk back |
 |
|
|
|