473,598 Members | 3,161 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 43964
SELECT *
FROM Transactions AS T
WHERE transactiondate =
(SELECT MAX(transaction date)
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******** ************@gi ganews.com...
SELECT *
FROM Transactions AS T
WHERE transactiondate =
(SELECT MAX(transaction date)
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,tra nsactiondate)

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
2586
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), username, date, and time field like this: 1234 williamstim 01AUG03 7:44:31 2348 williamstim 02AUG03 14:11:20
0
1502
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 just 1 entry per unit. I need to select the most recent record for each unit together with its extra data. I cannot see how to do this in a single select. At present I sort by date DESC and group by unit_id then my perl script discards all but...
2
1588
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: I have two tables, which relate to where a person works within an
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 identifying to which thread the message belongs. Examples below. Any ideas on the SQL I need to use here?
4
2936
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 MS SQL Server) that would return the last title published by every of the publishers. Quite clear situation. But I can't make it work. If I use inner join (which I should, because I need data from both tables) then I get a result showing all...
4
4712
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 athletes.  I have now learned how to join two different tables in a query so that I might generate a report. Specifically, student athletes at a community college are required to graduate with an AA or AS degree.  Consequently, various steps are in...
3
1911
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 function in QA and it seems to me that the two versions execute identically - that is, the addressing the sub-query of the WHERE clause with either IN or = results in the same execution plan. Does this seem plausible or did I misinterpret...
13
4013
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. It also has ItemNumber and LatestUsedDate. Table 2 may not have any record for an ItemNumber in Table 1; it may have one record; or it may have several records for a given ItemNumber. Sometimes the LatestUsedDate is filled in; sometimes...
4
1900
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 textbox etc will be filled with that specific record. Can i do this without using binding context?
0
7896
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8389
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5848
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1246
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.