Updating records but limiting to only one row per duplicate record

时光总嘲笑我的痴心妄想 提交于 2019-12-25 18:37:31

问题


I have a table with many item_number records which are duplicates within my table and the other columns within the table for each item_number are blank.

All of the records within the table are under product group of g024. However when I run my query it will update all of my items numbers with this.

What I want it to do is for each item_number only populate resource code, operation and operation description only once and not populating the remianing blank fields for each item_number with the criteria within the query.

I will populate these other black fields with a different update query later on.

UPDATE resource1 SET [resource code] ='TOOLASSY', OPERATION = '10',   OPERATION_DESCRIPTION     = 'MOULD TOOL ASSEMBLY'
WHERE [product group]="G024" 

I did try something like this to make the query only update each item_number only once. I looked into the TOP keyword but not sure if it was on the right line or not.

I need something like the DISTINCT keyword within a sub query for an update query.

UPDATE resource1 SET [resource code] ='TOOLASSY', OPERATION = '10', OPERATION_DESCRIPTION = 'MOULD TOOL ASSEMBLY'
WHERE [product group]="G024" AND ITEM_NUMBER IN(SELECT TOP 1 ITEM_NUMBER FROM RESOURCE1)

回答1:


Updated: UPDATE resource1 SET [resource code] ='TOOLASSY', OPERATION = '10', OPERATION_DESCRIPTION = 'MOULD TOOL ASSEMBLY' WHERE [product group]="G024" AND [num] IN (SELECT MinNum FROM (SELECT resource1.[product group], resource1.[item_number], MIN(resource1.[num]) AS MinNum FROM resource1 GROUP BY resource1.[product group], resource1.[item_number]) Q1 WHERE Q1.[product group]=resource1.[product group] AND Q1.[item_number]= resource1.[item_number]);

Tested in MSAccess 2010.



来源:https://stackoverflow.com/questions/19999309/updating-records-but-limiting-to-only-one-row-per-duplicate-record

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!