有表Akey1 month1 month2 month3 month4 ----------- ----------- ----------- ----------- ----------- 1 0 100 100 100 2 0 0 200 0 3 0 0 0 0 4 999 0 0 0 以上记录中,我需要加一个标志列,这个标志列记录着月份不等于0最大标志列,要的结果如下:key1 month1 month2 month3 month4 flag ----------- ----------- ----------- ----------- ----------- ----------- 1 0 100 100 100 4 2 0 0 200 0 3 3 0 0 0 0 0 4 999 0 0 0 1 使用计算列:create table T1( key1 int, month1 int, month2 int, month3 int, month4 int, flag as (case when month4>0 then 4 when month3>0 then 3 when month2>0 then 2 when month1>0 then 1 else 0 end) ) insert T1 select 1,0,100,100,100 union all select 2,0,0,200,0 union all select 3,0,0,0,0 union all select 4,999,0,0,0 select * from T1 key1 month1 month2 month3 month4 flag ----------- ----------- ----------- ----------- ----------- ----------- 1 0 100 100 100 4 2 0 0 200 0 3 3 0 0 0 0 0 4 999 0 0 0 1