Solved

SQL server delete duplicate rows but change a column to sum

Posted on 2015-01-07
4
205 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
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

840 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