Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
onglim
Starting Member
2 Posts |
Posted - 2011-05-03 : 12:42:00
|
| I am still new in sql, and I am stuck after finding outthe duplicate record. I need to sum up the duplicate record(order_qty), save record in temp table and delete the duplicaterecord from the table and insert back again the record tothe table from temp table. Appreciate the help given. LINE_NO ITEM_CODE CUSTOMER_PO ORDER_QTY ---------- ------------ -------------- ------------ 43 AB067 11001952 1 44 AB067 11001952 2 45 AB070 11001952 1 46 AG137 11001952 1 47 AB088 11001959 2 48 AE015 11001959 12 49 AG123 11001959 1 50 AG137 11001959 4 51 AG137 11001959 2 Above is the partial data from DNRECEIVETEMP tableI already found the duplicate recordSql statement I use to find out duplicate record:SELECT * FROM Dnreceivetemp c, (SELECT item_code, customer_po from DNRECEIVETEMP HAving count (item_code) > 1 and count (customer_po) > 1group by item_code, customer_po) d WHERE c.item_code = d.Item_codeand c.CUSTOMER_PO = d.CUSTOMER_PODuplicate record sorted out: LINE_NO ITEM_CODE CUSTOMER_PO ORDER_QTY ---------- ------------ -------------- ------------ 43 AB067 11001952 1 44 AB067 11001952 2 50 AG137 11001959 4 51 AG137 11001959 2 got 2 duplicate record (43-44) and (50-51)1 item can have only 1 Customer_PO (order no)1 Customer_po (order) can have many itemI want to change the Order_QTY on line 43 and 50 and delete line 44 and 51. LINE_NO ITEM_CODE CUSTOMER_PO ORDER_QTY ---------- ------------- -------------- ---------- 43 AG137 11001959 3 50 AB067 11001952 6Final result should be - Line_No 43 and 50 order_qty is update and Line_No 44 and 51 is delete LINE_NO ITEM_CODE CUSTOMER_PO ORDER_QTY ---------- ------------ -------------- ------------ 43 AB067 11001952 3 45 AB070 11001952 1 46 AG137 11001952 1 47 AB088 11001959 2 48 AE015 11001959 12 49 AG123 11001959 1 50 AG137 11001959 6 Assume that Line_no is not neededI need help for sql statement on:1. How to sum the order_qty2. How to put the result into temp table3. How to delete the duplicate record4. How to insert the data back to DNRECEIVETEMP from temp table |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-03 : 13:11:44
|
| select ITEM_CODE, CUSTOMER_PO, LINE_NO = min(LINE_NO), ORDER_QTY = sum(ORDER_QTY)into #afrom Dnreceivetempgroup by item_code, customer_pohaving count(*) > 1update Dnreceivetempset ORDER_QTY = a.ORDER_QTYfrom Dnreceivetemp tjoin #a aon t.item_code = a.item_codeand t.customer_po = a.customer_poand t.LINE_NO = a.LINE_NOdelete Dnreceivetempfrom Dnreceivetemp tjoin #a aon t.item_code = a.item_codeand t.customer_po = a.customer_poand t.LINE_NO <> a.LINE_NOYou could do this in a single statement with a cte and merge but it's probably easier like this.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
onglim
Starting Member
2 Posts |
Posted - 2011-05-04 : 02:59:35
|
| Thanks for giving the solution. Very helpful. Thanks |
 |
|
|
|
|
|
|
|