Author |
Topic |
chipembele
Posting Yak Master
106 Posts |
Posted - 2011-10-26 : 12:06:08
|
HiI'm wondering if anyone can help.I have a list of names with session dates against them. What I'm trying to do is create a SQL query that for the below example that would only return the earliest line for each person.RefNo Name Register Date123456 Smith Tutorial 17/09/2011123456 Smith Tutorial 18/09/2011123456 Smith Tutorial 01/09/2011123456 Smith Tutorial 08/09/2011123456 Smith Tutorial 15/09/2011234576 Jones Tutorial 02/09/2011234576 Jones Tutorial 15/09/2011234576 Jones Tutorial 16/09/2011So what I want to do is have a list like this as my resultsRefNo Name Register Date123456 Smith Tutorial 01/09/2011234576 Jones Tutorial 02/09/2011I tried using TOP but this just returned the earliest record (which was the smith 01/09/2011 one) not the earliest record for each person.Any advice appreciated.Dan |
|
X002548
Not Just a Number
15586 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2011-10-26 : 12:57:45
|
I am not sure what type of column your date field is. If it is a datetime then you can just use this query.select *from(slect row_number() over (partition by refno order by date asc) as rowid,* frommytable) awhere a.rowid = 1 But if it's a varchar field, I'd make sure I identified the format as DD/MM/YYYY to ensure proper sort so the format always converts properly.select *from(slect row_number() over (partition by refno order by convert(datetime,date,105) asc) as rowid,* frommytable) awhere a.rowid = 1 Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2011-10-26 : 13:45:29
|
Nah, that's the way I would've done it in SQL 2000, but I just figured the Rank functions in 2005+ are really helpful tools and it would benefit to show him how they can be used. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2011-10-26 : 14:15:33
|
I do not know the scope of his data, or have enough information to know if any difference in performance would have a noticeable effect with what he was doing. Based on what he has illustrated, the below query would be the best performance for the results he has shown, even if it is not specifically what he asked, and maybe this is all he was looking for:select RefNo, Name, Register, min(Date) as datefrommytablegroup by RefNo, Name, Register The Row_number and other ranking functions in most scenarios perform well and give a ton of flexibility (i.e. if he didn't want the first date, but instead decided he wanted to see the second lowest date). I was simply illustrating an option. I hope you did not interpret as saying there is something wrong with your query. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2011-10-26 : 15:37:16
|
Sounds good! Also I noticed on your query you have it set to MAX date, I think it was just a typo and he wanted the Min date.SELECT * FROM myTable o WHERE EXISTS (SELECT * FROM myTable i WHERE o.RefNo = i.RefNo GROUP BY RefNo HAVING MIN(i.Date) = o.Date) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2011-10-27 : 04:24:37
|
Thankyou for your responses. I'll have a go today. |
 |
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2011-10-31 : 08:39:49
|
ThankyouIn the end I used the ranking method as I was more familiar with that having used it on other tasks.RegardsDan |
 |
|
|