Get total of MYSQL table column across multiple records and insert total into new table

Posted on 2012-09-12
Last Modified: 2012-09-12
I have a customer_registration table that holds records of sometimes multiple products purchased by a customer (by customer_id). Each product entry has it's own service_length (INT) column to hold the number of support sessions for each product.

What, I'd like to do is total the service_length number across a customer_id's multiple records and then INSERT that total along with customer_id into a new table customer_support_sessions - Essentially tying the service_length total to each customer instead of tying it to each product.

I was thinking maybe it could be done with an INSERT INTO statement like below with some kind of a subquery to do the total COUNT for service_length across the multiple customer_id records?
INSERT INTO `customer_support_sessions` (customer_id, service_length)
SELECT cr.customer_id, cr.service_length
FROM customer_registration cr
WHERE whatever


Question by:Animaze
    1 Comment
    LVL 58

    Accepted Solution

    What is service_length? Just a number?
    If so you can just add sum to that field

    INSERT INTO `customer_support_sessions` (customer_id, service_length)
    SELECT cr.customer_id, sum(cr.service_length)
    FROM customer_registration cr
    WHERE whatever
    GROUP BY cr.customer_id

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now