?
Solved

SQL server delete duplicate rows but change a column to sum

Posted on 2015-01-07
4
Medium Priority
?
228 Views
Last Modified: 2015-01-07
I need to delete duplicates BUT not just delete duplicate, instead delete duplicates but SUM a column. the column to sum is called [value]
(Note I don't want query to DISPLAY the SUM, instead I want to DELETE duplicates)

columns in table are as follows [tblInputs]
example data (with duplicates)
[inputid] [PIE2_I_ID] [timeStampKey] [value]
-------------------------
1      1        1       5
2      1        1       4
3      1        1       9

4      1        2        1
5      1        2         3


result required (without duplicates):
-------------------------
[inputid] [PIE2_I_ID] [timeStampKey] [value]
1      1        1       18   (5+4+9)
4       1        2       4   (1+3)


thanks
0
Comment
Question by:rwallacej
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40535415
I would do this in 2 steps:
* create a intermediate table with the group/sum results
* delete the original table and reinsert back from that intermediate table

anything else comes back to non-set programming, which is to be avoided or just too complex to document

create table tmp_results
as
select min([inputid]) inputid, [PIE2_I_ID] , [timeStampKey] , sum([value]) value
 from yourtable
group by  [PIE2_I_ID] , [timeStampKey]
;
delete yourtable;
insert into yourtable (inputid, [PIE2_I_ID] , [timeStampKey] , value)
select inputid, [PIE2_I_ID] , [timeStampKey] , value
 from tmp_results
;
drop table tmp_results

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40535528
Looks like the above answer is correct.

The delete part of this question I cover in my article SQL Server Delete Duplicate Rows Solutions.  Based on this question I should add an 'not delete but sum based on columns' section to it.  Thanks.
0
 

Author Comment

by:rwallacej
ID: 40535575
for benefit of SQL Server users

--* create a intermediate table with the group/sum results
-- * delete the original table and reinsert back from that intermediate table
SELECT PIE2_I_ID, timeStampKey, SUM(value) AS value
INTO #tmp_results
FROM   tblInputs
GROUP BY PIE2_I_ID, timeStampKey
--
DELETE tblInputs

insert into tblInputs ([PIE2_I_ID] , [timeStampKey] , value)
select [PIE2_I_ID] , [timeStampKey] , value
 from #tmp_results
;
drop table #tmp_results
0
 

Author Closing Comment

by:rwallacej
ID: 40535576
thanks for help, much appreciated
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

770 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