Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2012-11-17 : 07:02:57
|
i have the following tablecreate table #data1(assetcenter int,hped int,model varchar(20),result int)-- INSERT DATAinsert into #data1 (assetcenter, hped,MODEL,result)select 0,1,'MODEL DL3',null union allselect 4,3 ,'MODEL R7',null union allselect 0,null,NULL,nullI 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 ENDFROM(select assetcenter, hped,MODEL,case when assetcenter = 0 and hped > 0 then hped when assetcenter > hped then hpedwhen assetcenter = 0 and hped is null then 1end as resultfrom #data1) t1However i want to update the result column USING UPDATE STATEMENT - How can i achieve that?OUTPUT REQUIRED:-------------------------0 1 MODEL DL3 14 3 MODEL R7 20 NULL NULL 1 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-17 : 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
Posting Yak Master
101 Posts |
Posted - 2012-11-18 : 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 DATAinsert into #data1 (assetcenter, hped,MODEL,result)select 0,1,'MODEL DL3',null union allselect 4,3 ,'MODEL R7',null union allselect 0,null,NULL,null union allselect 5,3,null,null union allselect 0,null,'model r7',null-- SELECTSELECT t1.assetcenter,t1.hped,t1.MODEL,CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 WHEN t1.result = 3 then 4ELSE t1.result ENDFROM(select assetcenter, hped,MODEL,case when assetcenter = 0 and hped > 0 then hped when assetcenter > hped then hpedwhen assetcenter = 0 and hped is null then 1end as resultfrom #data1) t1 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-11-19 : 01:55:28
|
just turn it into an updateUPDATE t1SET t1.RESULT=CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 WHEN t1.result1 = 3 then 4ELSE t1.result1 ENDFROM(select assetcenter, hped,MODEL,case when assetcenter = 0 and hped > 0 then hped when assetcenter > hped then hpedwhen assetcenter = 0 and hped is null then 1end as result1,RESULTfrom #data1) t1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2012-11-19 : 03:11:55
|
Thank you Visakh!!...It works fine :) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-11-19 : 03:25:38
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|