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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:21:21+00:00 2026-06-13T15:21:21+00:00

I’ve got a little time-tracking web app (implemented in Rails 3.2.8 & MySQL). The

  • 0

I’ve got a little time-tracking web app (implemented in Rails 3.2.8 & MySQL). The app has several users who add their time to specific tasks, on a given date. The system is set up so a user can only have 1 time entry (i.e. row) per task per date. I.e. if you add time twice on the same task and date, it’ll add time to the existing row, rather than create a new one. The user_id/task_id/date uniqueness is enforced by a UNIQUE index.

Now I’m looking to merge 2 tasks. In the simplest terms, merging task ID 2 into task ID 1 would take this

time  | user_id  | task_id  | date
------+----------+----------+-----------
10    | 1        | 1        | 2012-10-29
15    | 2        | 1        | 2012-10-29
10    | 1        | 2        | 2012-10-29
5     | 3        | 2        | 2012-10-29

and change it into this

time  | user_id  | task_id  | date
------+----------+----------+-----------
20    | 1        | 1        | 2012-10-29 <-- time values merged (summed)
15    | 2        | 1        | 2012-10-29 <-- no change
5     | 3        | 1        | 2012-10-29 <-- task_id changed (no merging necessary)

I.e. merge by summing the time values, where the given user_id/date/task combo would conflict.

I figure I can use a unique constraint to do a ON DUPLICATE KEY UPDATE ... if I do an insert for every task_id=2 entry. But that seems pretty inelegant.

I’ve also tried to figure a way to first update all the rows in task 1 with the summed-up times, but I can’t quite figure that one out.

Any ideas?


Update: Going off of Olaf’s answer below, I came up with this, which seems to be working

INSERT INTO `timetable`
(`time`, `user_id`, `task_id`, `date`)
(
    SELECT
      SUM(`time`) AS `time`,
      `user_id`,
      1 AS `task_id`,
      `date`
    FROM `timetable` AS `t1`
    WHERE `task_id` IN (1,2)
    GROUP BY `user_id`, `date`
)
ON DUPLICATE KEY UPDATE `time`=VALUES(`time`);

DELETE FROM `timetable` WHERE `task_id`=2;

I’ll leave the question open in case someone has a better solution (or if there are any gotchas in my solution that I should know about)

Update 2: Don’t know why I didn’t realize this earlier, but my solution may do a lot of redundant INSERTs, because it also finds all the entries that only exist in the target task and don’t need merging. In the above example data, the 2nd row will be found, re-inserted, trigger an on-duplicate-key, and set the time to the same as it already is. So if the target task has, say, 10 rows, and the source task 0 rows, it’ll still do 10 (completely pointless) INSERTs.

This can be avoided by wrapping the inner SELECT everything in yet another SELECT, and use COUNT(*) to only find those rows that need merging. Of course, this will necessitate another more queries to update the task_id of those rows that don’t need merging (which requires a join to figure out, as far as I can tell).

  • 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-13T15:21:22+00:00Added an answer on June 13, 2026 at 3:21 pm

    Summing on user_id and date would be:

    select sum(time), user_id, 1, date
    from timetable
    group by user_id, date;
    

    Updating the time table (kudos to @Flambino):

    insert into timetable (time, user_id, task_id, date)
        select sum(time), user_id, 1, date
        from timetable
        group by user_id, date
    on duplicate key update time = values(time);
    

    And finally remove all rows with task_id not 1:

    delete from timetable where task_id > 1;
    

    When updates are restricted to tasks 1 and 2, apply where clause like in @Flambino’s update.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
In my XML file chapters tag has more chapter tag.i need to display chapters

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.