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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:11:29+00:00 2026-05-28T20:11:29+00:00

I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL

  • 0

I have a table defined like this:

CREATE TABLE mytable (id            INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), 
                      user_id       INT REFERENCES user(id) ON UPDATE CASCASE ON DELETE RESTRICT,
                      amount        REAL NOT NULL CHECK (amount > 0),
                      record_date   DATE NOT NULL
                     );
CREATE UNIQUE INDEX idxu_mybl_key ON mytable (user_id, amount, record_date);

I want to write a query that will have two columns:

  • user_id
  • amount

There should be only ONE entry in the returned result set for a given user. Furthermore, the amount figure returned should be the last recoreded amount for the user (i.e. MAX(record_date).

The complication arises because weights are recorded on different dates for different users, so there is no single LAST record_date for all users.

How may I write (preferably an ANSI SQL) query to return the columns mentioned previously, but ensuring that its only the amount for the last recorded amount for the user that is returned?

As an aside, it is probably a good idea to return the ‘record_date’ column as well in the query, so that it is eas(ier) to verify that the query is working as required.

I am using MySQL as my backend db, but ideally the query should be db agnostic (i.e. ANSI SQL) if possible.

  • 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-28T20:11:30+00:00Added an answer on May 28, 2026 at 8:11 pm

    First you need the last record_date for each user:

    select user_id, max(record_date) as last_record_date
    from mytable
    group by user_id
    

    Now, you can join previous query with mytable itself to get amount for this record_date:

    select 
          t1.user_id, last_record_date, amount
    from 
          mytable  t1
      inner join
          ( select user_id, max(record_date) as last_record_date
            from mytable
            group by user_id 
          ) t2
        on  t1.user_id = t2.user_id 
        and t1.record_date = t2.last_record_date
    

    A problem appears becuase a user can have several rows for same last_record_date (with different amounts). Then you should get one of them, sample (getting the max of the different amounts):

    select 
          t1.user_id, t1.record_date as last_record_date, max(t1.amount)
    from 
          mytable  t1
      inner join
          ( select user_id, max(record_date) as last_record_date
            from mytable
            group by user_id 
          ) t2
        on  t1.user_id = t2.user_id 
        and t1.record_date = t2.last_record_date
    group by t1.user_id, t1.record_date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table defined like this CREATE TABLE OrderItemRoomings( OrderItemRoomingId int IDENTITY(1,1) NOT
I have a table defined like this: create table users ( id int(10), age
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
Imagine to have a table defined as CREATE TABLE [dbo].[Price]( [ID] [int] NOT NULL,
In MSSQL I have a table created like this: CREATE TABLE [mytable] (fkid int
I have a table defined by: CREATE TABLE bar_table ( _id INTEGER NOT NULL
We have a composite primary key for the site table defined below. Functionally, this
I've a table which is defined like this CREATE TABLE [dbo].[MyTable]( [MyTableId] [bigint] IDENTITY(1,1)
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
I have an SQL table defined as below: CREATE TABLE [TestComposite] ( ID int,

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.