| Author |
Topic  |
|
|
sachingovekar
Yak Posting Veteran
97 Posts |
Posted - 11/17/2012 : 07:02:57
|
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
5152 Posts |
Posted - 11/17/2012 : 09:46:15
|
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; |
 |
|
|
sachingovekar
Yak Posting Veteran
97 Posts |
Posted - 11/18/2012 : 09:27:32
|
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
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47146 Posts |
Posted - 11/19/2012 : 01:55:28
|
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 |
 |
|
|
sachingovekar
Yak Posting Veteran
97 Posts |
Posted - 11/19/2012 : 03:11:55
|
| Thank you Visakh!!...It works fine :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47146 Posts |
Posted - 11/19/2012 : 03:25:38
|
welcome
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|
|
|