?
Solved

Update A Column based on results of another column

Posted on 2013-11-26
4
Medium Priority
?
147 Views
Last Modified: 2014-05-27
In the same table "PropertyCalcs", I am try to update a column based a minimum value of another column.

I have PropertyID, OrderNumber, and First Inspection Date as columns.

For each ProptertyID, I need SQL to find the min(OrderNumber) and then write 'First Inspection Date' in the First Inspection Date column, when it finds the minimum OrderNumber for that PropertyID.

If you have any questions, please let me know and I'll be happy to answer them.
0
Comment
Question by:Erik Hauser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39678062
Air code.  I also do my own stunts..
UPDATE your_table
SET [First Inspection Date] = 'First Inspection Date'
FROM your_table yt
JOIN (
   SELECT ProptertyID, min(OrderNumber) as min_order_number
   FROM your_table
   GROUP BY PropertyID) yt_min ON yt.PropertyID = yt_min.PropertyID AND yt.OrderNumber = yt_min.min_order_number

Open in new window

0
 

Author Comment

by:Erik Hauser
ID: 39678085
JimHorn,

Thank you so much!  I was practically there in two separate queries and couldn't figure out how to join them.

Thanks, I'll try that!
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 39678147
;with cte as (select [first inspection date]
                      ,row_number() over ( partition by propertyid order by ordernumber) as rn
    from propertycalcs)
update  x
   set [first inspection date]='first inspection date'
 from cte as x
where rn=1
 and [first inspection date] <> 'first inspection date'
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39678594
I'd suggest storing just a bit flag that indicated this: you can translate it to the characters using a computed column.

isFirstInspectionDate bit

update ...
set isFirstInspectionDate = 1
...


ALTER TABLE PropertyCalcs
ADD [First Inspection Date] AS CASE WHEN isFirstInspectionDate = 1 THEN 'First Inspection Date' ELSE '' END
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

765 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