SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 using update or nested
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sachingovekar
Yak Posting Veteran

99 Posts

Posted - 11/17/2012 :  07:02:57  Show Profile  Reply with Quote
i have the following table

create table #data1
(
assetcenter int,
hped int,
model varchar(20),
result int
)

-- INSERT DATA
insert into #data1 (assetcenter, hped,MODEL,result)
select 0,1,'MODEL DL3',null union all
select 4,3 ,'MODEL R7',null union all
select 0,null,NULL,null


I can get the result column using the below statement:

SELECT t1.assetcenter,t1.hped,t1.MODEL
,CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 ELSE t1.result END
FROM
(
select assetcenter, hped,MODEL,
case
when assetcenter = 0 and hped > 0 then hped
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result
from #data1
) t1


However i want to update the result column USING UPDATE STATEMENT - How can i achieve that?

OUTPUT REQUIRED:
-------------------------


0 1 MODEL DL3 1
4 3 MODEL R7 2
0 NULL NULL 1

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/17/2012 :  09:46:15  Show Profile  Reply with Quote
Your result does not seem to agree with the query - you have "t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 " in the outer query, which would mean that for the first row, result would be 2, yet in your sample result, it is given as 1.

Ignoring that for the moment, unless there is some other reason to use subqueries, you can do the updates in a single query like shown below:
UPDATE #data1 SET
	result = 
		CASE
			WHEN model LIKE '%DL3%' OR model LIKE '%R7%' THEN 2
			WHEN assetcenter = 0 AND hped > 0 THEN hped
			WHEN assetcenter > hped THEN hped
			WHEN assetcenter = 0 AND  hped IS NULL THEN 1
		END;
Go to Top of Page

sachingovekar
Yak Posting Veteran

99 Posts

Posted - 11/18/2012 :  09:27:32  Show Profile  Reply with Quote
consider the below table and data.

the output of the select statement below should be achieved using only one update statement.

I WANT TO UPDATE THE "RESULT" column of #data1 table and the output should be the same as selct statement.

create table #data1
(
assetcenter int,
hped int,
model varchar(20),
result int
)

-- INSERT DATA
insert into #data1 (assetcenter, hped,MODEL,result)
select 0,1,'MODEL DL3',null union all
select 4,3 ,'MODEL R7',null union all
select 0,null,NULL,null union all
select 5,3,null,null union all
select 0,null,'model r7',null

-- SELECT
SELECT t1.assetcenter,t1.hped,t1.MODEL,
CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2
WHEN t1.result = 3 then 4
ELSE t1.result END
FROM
(
select assetcenter, hped,MODEL,
case
when assetcenter = 0 and hped > 0 then hped
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result
from #data1
) t1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/19/2012 :  01:55:28  Show Profile  Reply with Quote
just turn it into an update


UPDATE t1
SET t1.RESULT=
CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 
WHEN t1.result1 = 3 then 4
ELSE t1.result1 END
FROM
(
select assetcenter, hped,MODEL,
case 
when assetcenter = 0 and hped > 0 then hped 
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result1,RESULT
from #data1
) t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 11/19/2012 01:58:04
Go to Top of Page

sachingovekar
Yak Posting Veteran

99 Posts

Posted - 11/19/2012 :  03:11:55  Show Profile  Reply with Quote
Thank you Visakh!!...It works fine :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/19/2012 :  03:25:38  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000