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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:17:15+00:00 2026-06-10T10:17:15+00:00

I want to redistribute some values in a table in a database I manage.

  • 0

I want to redistribute some values in a table in a database I manage.

Firstname(varchar), Lastname(varchar), Date(date), Icecream_consumed(int)

Where the combination of Firstname, Lastname, and Date make the primary key for this table. Changing the structure of this table is not an option.

There are 80 total unique combinations of Firstname and LastName in the database but i want to get that number down to 5 and add some of the records with the smaller total Icecream_consumed to the ones with the highest total. So that only the top 5 FirstName and LastName combinations have all the Icecream_consumed count.

For example if I execute the query:

  SELECT Firstname, Lastname, SUM(Icecream_consumed) 
    FROM table_name 
GROUP BY Firstname, Lastname 
ORDER BY SUM(Icecream_consumed);

I want it to only have 5 results, not 80. How can I modify the records within the table to reflect this without manually updating each record? A simple update would not work as it would be inserting where existing records with the same primary key exist.

EDIT: I noticed that the column ID was kind of ambiguous. So I removed it from this example.

EDIT2: Here is an example:

Existing:

Firstname, Lastname, SUM(Icecream_consumed)

John  Doe   1500
Joe   Doe   1400
Alex  Foo   1111
John  Foo   1000
Ben   Foo    999
Sue   Cool   500
Bill  Smith  200
Ben   Smith  150

I want to change the tables so that the sum(icecream_consumed) values associated with sue, cool, bill smith, and ben smith are associated with john doe so that only 5 results are visible. However if i tried to update all records for sue cool to be john doe instead, there would be a conflict with the primary keys since they both might have values for icecream_consumed for the same dates.
The outcome might look like:

John  Doe   2000
Joe   Doe   1600
Alex  Foo   1261
John  Foo   1000
Ben   Foo    999
  • 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-10T10:17:16+00:00Added an answer on June 10, 2026 at 10:17 am

    Working example on SQL Fiddle: http://sqlfiddle.com/#!2/0e8ab/1

    Because MySql cannot run sub selects in update statements if the subselect is the same table as the one in the update, you need to first temporarily copy the data into another table – so DO NOT run this when data is still going into the database as it is not an atomic operation. You need to shut down any applications using the database first.

    -- Create a copy of the data 
    CREATE TABLE table_name_copy SELECT * FROM table_name;
    
    -- Update the rows with the most Icecream_consumed to have the total 
    UPDATE table_name t1 SET t1.Icecream_consumed = (
        SELECT SUM(t2.Icecream_consumed) 
        FROM table_name_copy t2 
        WHERE t2.Firstname = t1.FirstName AND t2.Lastname = t1.Lastname
    )
    WHERE id = (
        SELECT id 
        FROM table_name_copy t2 
        WHERE t2.Firstname = t1.FirstName AND t2.Lastname = t1.Lastname
        ORDER BY Icecream_consumed DESC LIMIT 1
    );
    
    -- Delete the other rows that don't have the new totalled Icecream_consumed 
    DELETE FROM table_name
    WHERE id != (
        SELECT t2.id 
        FROM table_name_copy t2 
        WHERE t2.Firstname = table_name.FirstName AND t2.Lastname = table_name.Lastname
        ORDER BY Icecream_consumed DESC LIMIT 1
    );
    
    -- Remove the copied table
    DROP TABLE table_name_copy;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want the function to sort the table by HP but if duplicate HPs then
Want to code a key pad for an calculator. What I want to make
want to make a check in my installer before starting installation if any other
I want to redistribute tomcat as part of my application. I'll be distributing a
I'm sorry I'm english a little. I want to redistribute .Net Framework3.5 SP1 using
I have a table which displays some names and each row contains 3 cells.
Want to insert all the Rows of a table in a new table with
Want to trigger itemClicked event for a QTreeWidget Item using the following code. connect(aTreeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(CallM(QTreeWidgetItem*
If I want to sell my ebook online, and want to make sure the
Want to remove all 0 placed at the beginning of some variable. Some options:

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.