Solved

SQL server delete duplicate rows but change a column to sum

Posted on 2015-01-07
4
201 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 142

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

810 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