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

  • SEARCH
  • Home
  • 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 8283105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:37:04+00:00 2026-06-08T10:37:04+00:00

I have two tables from which I’m trying to run a query to return

  • 0

I have two tables from which I’m trying to run a query to return the maximum (or top) transaction for each person. I should note that I cannot change the table structure. Rather, I can only pull data.

People

+-----------+
| id | name |
+-----------+
| 42 | Bob  |
| 65 | Ted  |
| 99 | Stu  |
+-----------+

Transactions (there is no primary key)

+---------------------------------+
| person     | amount | date      |
+---------------------------------+
| 42         | 3      | 9/14/2030 |
| 42         | 4      | 7/02/2015 |
| 42         | *NULL* | 2/04/2020 |
| 65         | 7      | 1/03/2010 |
| 65         | 7      | 5/20/2020 |  
+---------------------------------+

Ultimately, for each person I want to return the highest amount. If that doesn’t work then I’d like to look at the date and return the most recent date.

So, I’d like my query to return:

+----------------------------------------+
| person_id  | name | amount | date      |
+----------------------------------------+
| 42         | Bob  | 4      | 7/02/2015 | (<- highest amount)
| 65         | Ted  | 7      | 5/20/2020 | (<- most recent date)
| 99         | Stu  | *NULL* | *NULL*    | (<- no records in Transactions table)
+----------------------------------------+

SELECT People.id, name, amount, date
FROM People
LEFT JOIN (
    SELECT TOP 1 person_id
    FROM Transactions
    WHERE person_id = People.id
    ORDER BY amount DESC, date ASC
)
ON People.id = person_id

I can’t figure out what I am doing wrong, but I know it’s wrong. Any help would be much appreciated.

  • 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-06-08T10:37:10+00:00Added an answer on June 8, 2026 at 10:37 am

    You are almost there but since there are duplicate Id in the Transaction table ,so you need to remove those by using Row_number() function
    Try this :

    With cte as 
     (Select People,amount,date ,row_number() over (partition by People 
                                      order by amount desc, date desc) as row_num
      from Transac )
     Select * from People as a 
     left join cte as b
     on a.ID=b.People
     and b.row_num=1
    

    The result is in Sql Fiddle

    Edit: Row_number() from MSDN

    Returns the sequential number of a row within a partition of a result set, 
    starting at 1 for the first row in each partition.
    

    Partition is used to group the result set and Over by clause is used

    Determine the partitioning and ordering of the rowset before the
    associated window function is applied.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the query to select some columns from two tables, which must contain
OK I have two sql tables I need to query from which are product
I have two different tables from which I need to pull data blogs which
i have two tables i am trying to get information from. login table -
I have the following query where I combine two fields from two tables: <cfquery
I have two tables which I need to select all rows from where the
I have two tables both of which have columns StartDate and EndDate. I'm trying
I have two tables. I am trying to find rows in one table which
Hi I want to have two tables each have an INT id column which
I have this two tables from my program to manage stock: Articulos (items): id_articulo

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.