Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 380325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:58:41+00:00 2026-05-12T14:58:41+00:00

Say I have a table transactions that has columns acct_id trans_date and trans_type and

  • 0

Say I have a table “transactions” that has columns “acct_id” “trans_date” and “trans_type” and I want to filter this table so that I have just the last transaction for each account. Clearly I could do something like

SELECT acct_id, max(trans_date) as trans_date  
FROM transactions GROUP BY acct_id;

but then I lose my trans_type. I could then do a second SQL call with my list of dates and account id’s and get my trans_type back but that feels very cludgy since it means either sending data back and forth to the sql server or it means creating a temporary table.

Is there a way to do this with a single query, hopefully a generic method that would work with mysql, postgres, sql-server, and oracle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:58:42+00:00Added an answer on May 12, 2026 at 2:58 pm

    This is an example of a greatest-n-per-group query. This question comes up several times per week on StackOverflow. In addition to the subquery solutions given by other folks, here’s my preferred solution, which uses no subquery, GROUP BY, or CTE:

    SELECT t1.*
    FROM transactions t1
    LEFT OUTER JOIN transactions t2
      ON (t1.acct_id = t2.acct_id AND t1.trans_date < t2.trans_date)
    WHERE t2.acct_id IS NULL;
    

    In other words, return a row such that no other row exists with the same acct_id and a greater trans_date.

    This solution assumes that trans_date is unique for a given account, otherwise ties may occur and the query will return all tied rows. But this is true for all the solutions given by other folks too.

    I prefer this solution because I most often work on MySQL, which doesn’t optimize GROUP BY very well. So this outer join solution usually proves to be better for performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have table with following columns 1. Client - string. 2. Profit
Lets say I have table named Place with columns: placeId int not null auto_increment,
Say we have a table like this: **tablename** ID Name Size Date AtStore1 AtStore2
Say I have a table called xml that stores XML files in a single
Say I have this table schema. ID AccNo Amount Say I have this data
Lets say I have two tables, one for transactions, and another table who's primary
(Note: this is for MS SQL Server) Say you have a table ABC with
Using SQL2k5, I have a staging table that contains columns that will populate numerous
I have this jade file - if (transactions != ) table th Site Name
Let's say I have a Student and a School table. One operation that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.