Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Leigh79
Starting Member
28 Posts |
Posted - 2009-07-24 : 07:46:36
|
Hi GuysI have a table of bookings. Each booking can have several transactions within this table.What I would like to be able to do is for each Booking Number extract the last transaction that took place and the information on that row e.g From this table:ID | Booking ID | Transaction Date | Leadname | Adults-------------------------------------------------------1 | 123456 | 01/07/2009 | TBA | 02 | 123457 | 01/07/2009 | TBA | 03 | 123458 | 01/07/2009 | BROWN | 24 | 123456 | 02/07/2009 | SMITH | 05 | 123456 | 03/07/2009 | SMITH | 26 | 123456 | 03/07/2009 | SMITH | 37 | 123457 | 03/07/2009 | JONES | 2I would like to return:Booking ID | Transaction Date | Leadname | Adults--------------------------------------------------123456 | 03/07/2009 | SMITH | 3123457 | 03/07/2009 | JONES | 2123458 | 01/07/2009 | BROWN | 2How would I go about doing this?ThanksLeigh |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 07:51:58
|
[code]select [Booking ID] , [Transaction Date] , [Leadname] | [Adults]from bookings b inner join ( select [Booking ID], [Transaction Date] = max([Transaction Date]), [ID] = max([ID]) from bookings group by [Booking ID] ) m on b.[Booking ID] = m.[Booking ID] and b.[Transaction Date] = m.[Transaction Date] and b.[ID] = m.[ID][/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
Leigh79
Starting Member
28 Posts |
Posted - 2009-07-24 : 07:59:30
|
Hi khtanThanks for replying so quickly - I'll try this out and let you know how I get on! |
|
|
|
|
|