sql - What is best way to merge 2 select statement? -
i have sql server query statement this:
with (     select (             sum(case                      when (t1.price) > 0                         (t1.price)                     else 0                     end)             ) pr1         ,(             abs(sum(case                          when (t1.price) < 0                             (t1.price)                         else 0                         end))             ) pr2     dbo.price_table t1     )     ,b (     select (when(pr1 - pr2) < 0 abs(pr1 - pr2) else 0 end) res         ) select res b   in query, use 2 select statement achieve "res" column, want achieve "res" column in 1 select statement.
what best way merge 2 select statement 1 select statement query?
your calculation seems way complicated.  taking sum of positive values.  sum of negative values, using abs() make value positive, , subtracting result.  guess what?  same taking sum() of values in first place.
so, think statement equivalent:
select (case when sum(t1.price) < 0              abs(sum(t1.price))              else 0         end) dbo.price_table t1;      
Comments
Post a Comment