Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

sql - Updating records but limiting to only one row per duplicate record

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...