Solved

SQL server delete duplicate rows but change a column to sum

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now