| View previous topic :: View next topic |
| Author |
Message |
Abhi
Joined: 21 Aug 2007 Posts: 144
|
Posted: Thu Jul 02, 2009 6:38 am Post subject: get last records from group by query in sql |
|
|
Hi Everyone,
I am getting problem with group by clause in sql. Here is my sample query.
SELECT `id` , `host_id` , `guest_id`
FROM `users_chats`
id host_id guest_id
1 31 32
2 31 32
3 31 32
4 31 56
5 31 56
6 56 32
7 56 32
After Group by
SELECT id, host_id, guest_id
FROM `users_chats`
GROUP BY host_id, guest_id
id host_id guest_id
1 31 32
4 31 56
6 56 32
I need to group by from last records. but Group by is giving only first records.
This is what I need.
id host_id guest_id
3 31 32
5 31 56
7 56 32
Thanks in Advance...
Bye... |
|
| Back to top |
|
 |
Raghavendra
Joined: 21 Aug 2007 Posts: 215
|
Posted: Thu Jul 02, 2009 6:42 am Post subject: |
|
|
Hi Abhi,
I think you are using MySql. Because Sql Server will not allow non group by columns without any aggregate function.
Here is your query:
SELECT Max( id ) , host_id, guest_id
FROM `users_chats`
GROUP BY host_id, guest_id
Result
Max( id ) host_id guest_id
3 31 32
5 31 56
7 56 32
Bye.... |
|
| Back to top |
|
 |
|