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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:57:58+00:00 2026-06-13T18:57:58+00:00

I have two tables customer_master > cname > lastreceiptdate > lastreceiptamt accounts > cname

  • 0

I have two tables

customer_master
> cname
> lastreceiptdate
> lastreceiptamt

accounts
> cname
> date
> amount

I need help in constructing a single update query. where the customer_master table is updated with the latest receipt date and receipt amount for a single customer code (cname like “FRUITXXXXX”) from accounts table.

It should work in mysql 5.0 and postgresql 9.0

thanks

so far we are using a select command to retrieve a record with max(Date) and then using another update command to update using results from the select query.

update customer_master 
set receiptdate = @_da, receiptamt=@_amt 
where cname = @_cn and (receiptdate < @_da or receiptdate='null') 
  • 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-13T18:57:59+00:00Added an answer on June 13, 2026 at 6:57 pm

    If you need to do this for all customers, it will be a lot faster to truncate the table and insert everything in one go:

    truncate table customer_master;
    insert into customer_master (cname, lastreceiptdate, lastreceiptamt)
    select cname, last_date, last_amount
    from amounts
      join (
        select cname, 
               max(date) as max_date
        from accounts
        group by cname
      ) ma on ma.max_date = amounts.date 
          and ma.cname = amounts.cname
    

    This asumes that the max. date is “unique” i.e. that not two rows with the same date exists in accounts.

    If you really, really want to update the table each time something changes, you can use something like this:

    update customer_master
        set lastreceiptdate = last_date, 
            lastreceiptamt = last_amount
    from (
      select cname, last_date, last_amount
      from amounts
        join (
          select cname, 
                 max(date) as max_date
          from accounts
          group by cname
        ) ma on ma.max_date = amounts.date 
            and ma.cname = amounts.cname
    ) as t
    where customer_master.cname = t.cname
    and customer_master.cname = 'some customer name';
    

    The customer_master.cname = 'some customer name' runs the update for a single customer, not for all. (I don’t know if that is going to work in MySQL though)

    Note that you do need to JOIN on the group by statement in order to get the correct amount that “belongs” to the latest receiptdate.

    If you use a simple

    select cname,
           max(date), 
           max(amount)
    from accounts;
    

    the returned max. amount does not necessarily “belong” to the max. date (it could be an amount from a different date).

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

Sidebar

Related Questions

I have two tables, Customer and CustomerPhone. Single record in Customer can have multiple
I have two tables, table A : Customer_ID Product Date Of Sale Pay Meth
I have two tables and I need to establish a 1-many relation among them,
I have two tables: orders: orderid customer_id amount 1 5 1 2 5 2
I have the following two tables: Customer { int Id int Name } Bills
I have two tables in my database that look like that: Customer: C_ID city
I have two tables, one with products and a foreign key for the customer.
If I have two tables, Customers and Orders, and I want to look up
I have two tables images2 and image_data So the goal is to have 1
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4

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.