[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Statement to Update Rows on Condition

Posted on 2013-12-09
4
Medium Priority
?
268 Views
Last Modified: 2013-12-09
I have duplicate rows in a table. All column values are equal except one numeric column.
I need to set a column value for the lowest Col3 value in each group.

Col1         Col2           Col3             Col4

1808      AA      30175130          1   Set Col4 to 0 for the min Col3 value of the AA Group
1808      AA      30176132          1
4450      AAN      30175454          1   Set Col4 to 0 for the min value of the AAN Group
8384      AAN      30175787          1
8385      AAN      30175788          1
4450      AAN      30177864          1
8384      AAN      30178679          1
8385      AAN      30178680          1

How can this be done?

Thanks,
0
Comment
Question by:Dovberman
  • 3
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39706084
Give this a whirl... a JOIN on your_table (rename to suit your needs) and a subquery that gets the minimum value of Col3 based on the Col2 value.
UPDATE your_table
SET Col4 = 0
FROM your_table yt
   JOIN (SELECT Col2, Min(Col3) as Col3_min
            FROM your_table
            GROUP BY Col2) yt_min 
   ON yt.Col2 = yt_min.Col2 AND yt.Col3 = yt_min.Col3_min

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39706234
Thank you,

I will try this later today and reply.
0
 

Author Comment

by:Dovberman
ID: 39706532
Works perfectly,

Thanks,
0
 

Author Closing Comment

by:Dovberman
ID: 39706534
Works perfectly,

Thanks,
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

834 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