473,836 Members | 2,178 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 43983
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
2593
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
1515
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
1600
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
2947
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
4742
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
1920
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
4030
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
1906
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
9656
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
10821
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
10527
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...
1
10573
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
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...
1
7773
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
6973
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.