473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to pull the most recent transaction based on entry date?

Hi -

Thanks for your help with this one.

I have a table that records transactions for individual invoices, and I want to pull the most recent transaction based on the entry date. Using the example below, I want to write a SQL statement that would only pull records that have ID 3,4, and 9 (as those records have the most recent EntryDate for each individual Invoice). Here is an example of the transaction table:


ID InvoiceNo EntryDate TransactionCode
------- ------------ ----------- -------------
1 000024832 8/30/2010 00038
2 000024832 9/1/2010 00055
3 000024832 11/20/2010 00078
4 000037852 11/1/2010 00024
5 000037852 10/30/2010 00124
6 000037852 8/7/2010 00226
7 000037852 8/5/2010 00365
8 000042783 9/7/2010 00038
9 000042783 10/4/2010 00356
10 000042783 8/20/2010 00226
Dec 2 '10 #1

✓ answered by ck9663

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. with most_recent_record
  3. as
  4. (
  5. select ID, max(EntryDate) as latest_entry_date from YourTable group by ID
  6. )
  7. Select * from YourTable a
  8. where exists (select 1 from most_recent_record b where a.id = b.id and a.entrydate = latest_entry_date)
  9.  
  10.  
Happy Coding!!!

~~ CK

3 3479
ck9663
2,878 Expert 2GB
Try:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select ID, max(EntryDate) from YourTable group by ID
  3.  
  4.  
Happy Coding!!!

~~ CK
Dec 2 '10 #2
Thank you for your response, but I forgot to mention that I wanted to pull the EntryDate, InvoiceNo, AND TransactionCode. I have tried the code below, but it pulls each record.

Expand|Select|Wrap|Line Numbers
  1. SELECT InvoiceNo, MAX(EntryDOS), TransactionCode
  2. FROM DbTable
  3. GROUP BY InvoiceNo, TransactionCode
  4.  
Dec 2 '10 #3
ck9663
2,878 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. with most_recent_record
  3. as
  4. (
  5. select ID, max(EntryDate) as latest_entry_date from YourTable group by ID
  6. )
  7. Select * from YourTable a
  8. where exists (select 1 from most_recent_record b where a.id = b.id and a.entrydate = latest_entry_date)
  9.  
  10.  
Happy Coding!!!

~~ CK
Dec 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tim Morrison | last post by:
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.
2
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2...
4
by: doublej0 | last post by:
I have the following issue when trying to write a SQL statement to accomplish my goal. I have done a lot of research, but have been unsuccessful in creating the query. I have two tables, one with...
1
by: evenprimes | last post by:
Here's the table setup: table1: -key -name table2: -key -modification_date -phone_number
1
by: mlwils2 | last post by:
I'm working with a report that uses three views. There are duplicate records because the 'priority' which comes from one view has changed and SELECT DISTINCT sees it as a separate record. The users...
1
by: Jonathan | last post by:
I have the following query which is retrieving a set of data it is almost what I want but I can not manage to get the result I desire. SELECT r1, r1_dev, r2, r2_dev, date, time FROM output...
9
by: psuaudi | last post by:
i have a table with dates that events occured. it looks something like this: 11/18/2006 1:00PM Open 11/18/2006 1:25PM Close I created a select query that selects the last (most recent)...
7
by: brewer95 | last post by:
I use a form to enter data into a table. The user then has the opportunity to recall the data in the form based on a search to review and update the info. For example, on my menu there is a combo...
1
by: jpgoeth | last post by:
What I'm trying to do is select the most recent record for each customer who created an account between 2004-01-01 and 2007-01-01. I'm using MSQuery. I have this so far: SELECT OrderOrAdjust,...
4
by: luciegiles | last post by:
Hi, I would like to order the records in a subform frmSub by the field 'ReferralDate' (short date) so that the most recent record appears first. I'm sure this must be fairly simple but I can't get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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...

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.