Rearranging values in 3 columns in SQL Server in descending order -


i want rearrange values in 3 columns (a, b, c) in descending order , have written logic below. logic works of times not working when "c" highest number

select      case        when > b , > c         when b > , b > c b         else c       end a,     case        when b > c , > b b         when c > , c < b c         else      end b,      case         when b > c , > c c         when < c , < b         else b      end c 

one way of doing be

select ca.a,        ca.b,        ca.c   t        cross apply (select max(case when rn = 1 val end) a,                            max(case when rn = 2 val end) b,                            max(case when rn = 3 val end) c                       (select row_number() on (order val) rn,                                    val                               (values (a),                                            (b),                                            (c)) v1(val)) v2) ca  

if columns nullable sort nulls first.

to sort them @ end change order by order case when val null 1 else 0 end, val

sql fiddle


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -