SQL - Capture result of where comparison as ad hoc column -
i've got sql question. want use case logic determine value of ad hoc column based on result of comparisons done in clause. simplified, want this:
select t.id, (case when cond1 "lbl1" + t2.v1 when cond2 "lbl2" + t2.v1) tbl1 t left join tbl2 t2 ( cond1 || cond2 )
the problem don't want recompute cond1 , cond2 in select clause, they're expensive.
how can result?
thanks, frood
this written in ms sql 2008, not sure if it'll work in other rdbms same code, gives idea of might work. have iterated each record, overall have run each comparison once instead of twice each record.
declare @cond1 bit declare @cond2 bit if 1=1 -- substitute condition 1 set @cond1 = 1 else set @cond1 = 0 if 1=0 -- substitute conditon 2 set @cond2 = 1 else set @cond2 = 0 select case when @cond1 = 1 'condition 1 met' when @cond2 = 1 'condition 2 met' end @cond1 = 1 or @cond2 = 1
Comments
Post a Comment