| Author |
Topic |
|
flion
Starting Member
8 Posts |
Posted - 2010-04-27 : 06:46:38
|
Hi,I've got a problem with my sql query, I need a result like this:Pete, 27-04-2010, 15658, NJeffrey, 27-04-2010, 57954, NMike, 26-04-2010, 79851, NPeter, 25-04-2010, 684587, YMy database looks like this:Name, date, points, alonePete, 25-04-2010, 1896, YJeffrey, 25-04-2010, 368, YMike, 24-04-2010, 245, NPeter, 23-04-2010, 657, YPete, 26-04-2010, 1248, NJeffrey, 26-04-2010, 218, YMike, 25-04-2010, 878, YPeter, 24-04-2010, 65, YPete, 27-04-2010, 15658, NJeffrey, 27-04-2010, 57954, NMike, 26-04-2010, 79851, NPeter, 25-04-2010, 684587, YSo I need to retrieve all the persons once, with the most recent info (date, points, alone).I hope I made myself clear? Many thanks in advance! |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-27 : 06:50:11
|
selectName,max(date) as Datefrom tablegroup by Name No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
flion
Starting Member
8 Posts |
Posted - 2010-04-27 : 08:07:10
|
quote: Originally posted by webfred selectName,max(date) as Datefrom tablegroup by Name No, you're never too old to Yak'n'Roll if you're too young to die.
Then I receive an error that it is not contained in either an aggregate function or the GROUP BY clause. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-27 : 08:15:48
|
Huh?Can you show your statement that errors? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
flion
Starting Member
8 Posts |
Posted - 2010-04-27 : 13:12:03
|
quote: Originally posted by webfred Huh?Can you show your statement that errors? No, you're never too old to Yak'n'Roll if you're too young to die.
Looks that I just made a mistake. I will check it tomorrow.Thanks for your help mate! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 13:46:51
|
quote: Originally posted by flion
quote: Originally posted by webfred Huh?Can you show your statement that errors? No, you're never too old to Yak'n'Roll if you're too young to die.
Looks that I just made a mistake. I will check it tomorrow.Thanks for your help mate!
I think you've used someother additional fields in select. once you group by a field you cannot directly use other fields in select. you need to apply aggregate functions on them.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
flion
Starting Member
8 Posts |
Posted - 2010-04-27 : 14:28:18
|
| Hmm, just tried this:select Name, max(date) as Date, points, alonefrom tablegroup by Name, points, aloneBut now I get too much records. When I use just Name and date it works flawless. But I need more info then just those 2. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-27 : 14:32:50
|
| selectname, date, points, alone from (select name, date, points, alone, row_number() over(partition by name order by date desc) as rnk from table)twhere rnk = 1 |
 |
|
|
flion
Starting Member
8 Posts |
Posted - 2010-04-27 : 15:10:35
|
| That looks exactly what I need. Many thanks! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 06:04:18
|
or you earlier query with this additionselect t.Name,t.Date, t.points, t.alonefrom table tjoin(select Name, max(date) as Datefrom tablegroup by Name)t1on t1.date=t.dateand t1.Name=t.Name ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|