sql server - Simplify SQL Query using variable -
i want simplfy t-sql query. :
select t.a [column a], t.b [column b], t.c [column c], (t.a - t.b - t.c) [column d], case when (t.a - t.b - t.c) = 0 'equals' when (t.a - t.b - t.c) > 0 'greater' when (t.a - t.b - t.c) < 0 'less' end [status] table1 t;
it nice put (t.a - t.b - t.c)
variable, can reuse on places occurs, because expression may change on time. not figure out how without changing existing query significantly.
use cross apply
select t.a [column a], t.b [column b], t.c [column c], [column d], case when [column d] = 0 'equals' when [column d] > 0 'greater' when [column d] < 0 'less' end [status] table1 t cross apply (select t.a - t.b - t.c [column d]) t2
Comments
Post a Comment