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 6381763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:13+00:00 2026-05-25T02:26:13+00:00

Here is a simplified transactions table. Each row is a transaction that has a

  • 0

Here is a simplified transactions table.

Each row is a transaction that has a unique transac id (identity field if you will), an accountpointer (foreign key to the accounts table which is not displayed here), a transac date and a dollar amount.

Sample data below:

trans_id acc_ptr trans_date amount
1        12      2011-08-24 2.0
2        12      2011-08-25 3.0
3        14      2011-07-28 -3.0
4        16      2011-06-14 -1.0
5        12      2011-05-15 0.5
6        16      2011-07-30 -2

What I want is very simple. Display the most recent transac grouped by acc_ptr including the amount for that date.

What I have works perfectly, but what I would like to know is, is there a more elegant way (as far as programming goes) or more efficient way of dealing with this problem, specifically my treatment of the subquery for amount? Would like to see how you would approach it.

My approach:

select acc_ptr
, max(trans_date) as [most rec transac date]
, (select amount from transactions t2 
where t2.trans_date = max(t1.trans_date) 
and t2.acc_ptr = t1.acc_ptr) as amount
from transactions t1
group by acc_ptr
  • 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-25T02:26:14+00:00Added an answer on May 25, 2026 at 2:26 am

    The first alternative that comes to mind is to use analytics (IE: ROW_NUMBER), but that’s SQL Server 2005+ functionality.

    WITH example AS (
      SELECT t.acc_ptr,
             t.trans_date AS [most rec transac date],
             t.amount,
             ROW_NUMBER() OVER (PARTITION BY t.acc_ptr 
                                    ORDER BY t.trans_date DESC) AS rnk
        FROM transactions t)
    SELECT e.acc_tpr,
           e.trans_date,
           e.amount
      FROM example e
     WHERE e.rnk = 1
    

    There’s no performance value in this example to using a CTE (WITH syntax) – this is equivalent:

    SELECT e.acc_tpr,
           e.trans_date,
           e.amount
      FROM (SELECT t.acc_ptr,
                   t.trans_date AS [most rec transac date],
                   t.amount,
                   ROW_NUMBER() OVER (PARTITION BY t.acc_ptr 
                                          ORDER BY t.trans_date DESC) AS rnk
              FROM transactions t) e
     WHERE e.rnk = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got 3 tables that are something like this (simplified here ofc): users user_id
I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
Here is a simplified table structure: TABLE products ( product_id INT (primary key, auto_increment),
I'm embedding here a simplified version of a code that is not working for
I have a DataGrid that looks like this (slightly simplified here): <asp:DataGrid ID=grdQuotas runat=server
Here is simplified version of my requirement I have a java class say Processor
I want to list all users with their corropsonding user class. Here are simplified
Here's a simplified version of what I'm trying to do : Before any other
Here is a simplified version of my database model. I have two tables: Image,
Here is a simplified version of my application showing what I'm doing. /* in

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.