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 |
|
LLatinsky
Starting Member
38 Posts |
Posted - 2003-06-09 : 11:46:53
|
| -- this is a simplified version of a query I am trying to write.-- for any given value of "a" I need to get a value of "b" that --corresponds to 'true',-- in case there is no 'true' than 'false'. Only when there's --no 'true' or 'false'-- get the value of 'unknown'. -- right now I can achieve this only with having separate queries to --test the condition-- any help will be greatly appreciatedcreate table table1(a int, b int)goinsert table1select 1,123union allselect 1, 234union allselect 1 , 567union allselect 1, 678union allselect 2,123union allselect 2, 678union all select 3, 456gocreate table table2(b int, c varchar(10))goinsert table2select 123,'true'union allselect 234,'false'union allselect 567,'unknown'go-- this is my shot at it but obviously it doesn't work correctlydeclare @b intselect @b =isnull(x.b,isnull(y.b,z.b))from table1 t left join (select b from table2where c='true')x on t.b=x.b left join (select b from table2where c='false')y on t.b=y.b left join (select b from table2where c='unknown')z on t.b=z.b where a=1 and t.b in (select b from table2 where c in ('true','false','unknown'))select @bgodrop table table1godrop table table2go-- thank you |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-06-09 : 12:19:29
|
| Something likedeclare @b int select top 1 @b = t.bfrom table1 tjoin table2 t2on t2.b = t.bwhere t.a=1order by case when t2.c = 'true' then 1when t2.c = 'false' then 2when t2.c = 'unknown' then 3endselect @b==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
LLatinsky
Starting Member
38 Posts |
Posted - 2003-06-09 : 12:44:56
|
| Thank you, nr. Just what I needed |
 |
|
|
|
|
|
|
|