Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine Like rows

Status
Not open for further replies.

skyleh

MIS
May 19, 2005
5
0
0
US
I need to modify a program and can't wrap my brain around how to do it. The program splits percentage spreads based on codes for a person. Sometimes the same code may show up twice and the program inserts two entries for the table for that code with two different percentages instead of combining them. It reads one at a time figures the percentage and adds it to the table.

I can go back and find them - would like like this
Code Percentage UniqueID
123 15 12345
124 10 12346
123 75 12347

I would either like to do it before the initial insert or go back and update and not sure how to do it.

It should look like this

Code Percentage UniqueID
123 90 12345
124 10 12346

I was thinking to build two cursors and loop through the top one to insert into the table and loop through the second one looking for the duplicates. But I don't know if I can delete the found duplicate within the cursor? ... or is there a better way of doing it.

Any help would be greatly appreciated.
 
To get:
Code Percentage UniqueID
123 90 12345
124 10 12346
From:
Code Percentage UniqueID
123 15 12345
124 10 12346
123 75 12347

SELECT Code, Sum(Percentage), Min(UniqueID)
FROM yourTable
GROUP BY 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ok I am trying to make it harder than it is.

I can do this..

SELECT Code, Sum(Percentage), Min(UniqueID)
FROM myTable
GROUP BY 1

Then delete the rows in myTable and do insert my results back into yourTable to make sure those rows are the only ones there.

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top