Solved

SQL server delete duplicate rows but change a column to sum

Posted on 2015-01-07
4
209 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 500 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 65

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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…

730 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