Author |
Topic |
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-11-20 : 12:05:48
|
Hi there,Table1 has coulmns of CodeId, TypeId and ModelId, Num1, Num2 ... the Num1, Num2 have data type of int values and the combination of CodeId, TypeId and ModelId can't be the same. In other word, those CodeId, TypeId and ModelId combination must be unique. If any same combination ID found then add up Num1 and Num2 and save the sum as record then old records must be removed. See following sample:Rec#|CodeId|TypeId|ModelId|Num1|Num2| ...1 |11 | 13 | 3 | 24 | 18 |2 |13 | 9 | 2 | 17 | 21 |3 |11 | 13 | 3 | 29 | 65 |4 |14 | 13 | 17 | 24 | 18 |From above table Rec# 1 and 3 have same ID combination. So Rec#1 and 3 must be removed and a new rec with same ID combination and Num1=53, Num2=83 should be added before removal. New rec looks like this:5 |11 | 13 | 3 | 53 | 83 |How can I get that? Thanks in advance. |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-20 : 12:17:53
|
Try this in test environment before you apply:-- Insert into Temp tableSelect CodeId,TypeId,ModelId,SUM(Num1)NUM1,SUM,(Num2)NUM2........into #tempfrom TableGroup by CodeId,TypeId,ModelId-- Delete the entriesDelete P from(Select CodeId,TypeId,ModelId,ROW_NUMBER() OVER (PARTITION BY CodeId,TypeId,ModelId Order by CodeId) as Seqfrom Table)PWhere P.Seq > 1-- Reinsert backInsert into tableSelect CodeId,TypeId,ModelId,NUM1,NUM.....from #temp |
|
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-11-20 : 13:21:54
|
Thanks sodeep. I think the 2nd part of deletion, the P.Seq should be greater than 0 not 1. If P.Seq > 1 it only deletes the duplicated records not all. Later when it comes to 3rd part, write back the #temp table it should write to an empty table.I am thinking about if it's possible that getting data for only duplicated records in 1st part because the table is huge, about millions records there.Anyway thanks a lot again. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-20 : 13:36:00
|
I hope RecNo is and Identity, if not, disregardCREATE TABLE #temp (RecNo INT IDENTITY (1,1),CodeID Int,TypeID int,ModelID int,Num1 int,Num2 int)INSERT INTO #temp VALUES (11 , 13 , 3 , 24 , 18) ,(13 , 9 , 2 , 17 , 21) ,(11 , 13 , 3 , 29 , 65 ),(14 , 13 , 17 , 24 , 18 )SELECT *FROM #tempSELECT DISTINCT t1.CodeID,t1.TypeID,t1.ModelID,SUM(Num1) OVER(PARTITION BY t1.CodeID,t1.TypeID,t1.ModelID) AS NewNum1,SUM(Num1) OVER(PARTITION BY t1.CodeID,t1.TypeID,t1.ModelID) AS NewNum2 ,COUNT(*) OVER(PARTITION BY t1.CodeID,t1.TypeID,t1.ModelID) AS NumRowsINTO #temp_GroupsFROM #temp t1 DELETE tgtFROM #temp tgtINNER JOIN #temp_Groups src ON tgt.CodeID = src.codeID AND tgt.typeId = src.typeid AND tgt.ModelID = src.modelIDWHERE src.NumRows > 1 INSERT INTO #temp(CodeID ,TypeID ,ModelID,Num1,Num2)SELECT CodeID t,TypeID ,ModelID,NewNum1,NewNum2FROM #temp_GroupsWHERE NumRows>1SELECT * FROM #tempDROP TABLE #tempDROP TABLE #temp_GroupsJimEveryday I learn something that somebody else already knew |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-20 : 13:48:38
|
[code]-- Insert into Temp tableSelect CodeId,TypeId,ModelId,Count(*),SUM(Num1)NUM1,SUM,(Num2)NUM2........into #tempfrom TableGroup by CodeId,TypeId,ModelIdHaving Count(*) > 1-- Delete the entriesDelete t fromtable tinner join #temp p on p.CodeId = t.CodeId and p.TypeId= t.TypeId and p.ModelId = t.ModelId-- Reinsert backInsert into tableSelect CodeId,TypeId,ModelId,NUM1,NUM.....from #temp[/code] |
|
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-11-20 : 14:47:34
|
Thanks guys. jim, the select distinct part adds all the columns, even the ones without duplicated.sodeep, this time it works perfect!!! That Having Count(*) > 1 clears up the puzzle and deletion hits on only the duplicated combination. One more question: I use this in a store procedure to insert a new record to table, my question is should I embed the code into the stored procedure at the end or put it into the trigger of table? which one gives the best performance?Thank you guys again!!! |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-20 : 14:53:00
|
I wouldn't use trigger in this case. |
|
|
|
|
|