Looping through a table and adding a column

Posted on 2009-04-30
Last Modified: 2012-05-06
I need some assistance with the sql to accomplish the desired outcome.  
Here is an example of what the data could look like in the table.
Table X
   L_ID                        C_ID          Indem
 32566                      54345        25.00
 32566                      54346       26.00
 32566                      54346        37.00
 32566                      54347         45.00

I will be writing this data to another table.  What I will be doing is taking everthing from the specified table X with the same L_ID and inserting it into another table.  The one thing that will change is that I need to add the Indem column when the C_ID is equal to another C_ID.  So with that said the C_ID with 54346  will need to have their Indem values added together and inserted into table Y.  
Question by:oak29
    LVL 39

    Accepted Solution

    insert into to_table select l_ID, C_ID, sum(indem) from  from_table group by l_id, c_id

    Author Closing Comment

    Thanks!  Wow, that was a lot easier than I made it out to be.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now