473,499 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting Most Recent Record

MSSQL2000

I have a table that contains customer transactions
CustomerID
Transaction
TransactionDate
....

I need to select the most recent record that matches a specific CustomerID.

I am fairly new to SQL, could someone provide a sample select statement.

TIA

Tim Morrison

--
Tim Morrison

--------------------------------------------------------------------------------

Vehicle Web Studio - The easiest way to create and maintain your vehicle related website.
http://www.vehiclewebstudio.com
Jul 20 '05 #1
3 43947
SELECT *
FROM Transactions AS T
WHERE transactiondate =
(SELECT MAX(transactiondate)
FROM Transactions
WHERE customerid = T.customerid)

Remember that, unless (customerid, transactiondate) is unique, you may still
get more than one row for a Customerid if there are multiple rows with the
same maximum date.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
TransactionDate contains both a Date and a Time, that being said, there can
NEVER be a record with an EXACT date/time/customer match.

Does MAX take the time part of the field into consideration?

Tim Morrison

"David Portas" <RE****************************@acm.org> wrote in message
news:a5********************@giganews.com...
SELECT *
FROM Transactions AS T
WHERE transactiondate =
(SELECT MAX(transactiondate)
FROM Transactions
WHERE customerid = T.customerid)

Remember that, unless (customerid, transactiondate) is unique, you may still get more than one row for a Customerid if there are multiple rows with the
same maximum date.

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #3
> TransactionDate contains both a Date and a Time, that being said, there
can
NEVER be a record with an EXACT date/time/customer match.
In that case make sure you define a constraint on the table if you haven't
already done so:
UNIQUE (customerid,transactiondate)

Does MAX take the time part of the field into consideration?

Yes
--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2575
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
0
1494
by: Steve | last post by:
I have a table containing fields unit_id, temperature and date. This has data for around 20 units, with many 1,000's of records for each. Further information on each unit is in another table with...
2
1583
by: Mark Hargreaves | last post by:
Please help!!! I have a problem with a query that I cannot seem to resolve!! First things first, I am using MySQL version 4.0.17, which means I cannot use subselects. Here is my problem: ...
1
646
by: Ben Gribaudo | last post by:
Hello! I am trying to query a MySQL table and retrieve either the earliest or the latest message in each thread. The messages are in a table with MsgID being the primary key and fThreadID...
4
2928
by: Lucius | last post by:
Hello everyone, I have a query problem. I'll put it like this. There is a 'publishers' table, and there is a 'titles' table. Publishers publish titles (of course). Now I want to make a query (in...
4
4699
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
3
1892
by: Beowulf | last post by:
I have the following function(s) that each joins an active record with it's most recent record in an audit trail table, to show differences. I've perused the execution plans for 2 versions of this...
13
3996
by: angi35 | last post by:
Hi - working in Access 2000... my goal is to combine fields from two tables in a query: Table 1 has ItemNumber and ItemDescription. There's only one record per ItemNumber. Table 2 has ItemAlias....
4
1893
by: techno58 | last post by:
In my form i want to be able to fill all my controls with a particular record by selecting that record from my datagridview.If i select a particular record after clicking on it all my controls like...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.