请教下大家:一个表中有4列:xh,a,b,c,现在要根据这个表查询xh的完成情况:如果c>a,则xh已完成,否则如果b+c>=a,则xh生产中,否则xh未生产,该怎么查询生成一个表xh,wc(完成情况)?

解决方案 »

  1.   

    select 
    case when c>a then 'xh已完成' when b+c>=a then 'xh生产中' else 'xh未生产' end
    from tbName
      

  2.   

    但是b+c>=a是把c>a排除后,就是如果c>a一种情况,否则如果b+c>=a一种情况,然后在否则。类似与if then ...
          elseif then ...
          else ...
          end if
      

  3.   

    举个例子:
    表table1
    xh     a    b    c
    001    3    0    4
    002    4    1    1
    003    4    2    3
    查询得到表table2
    xh     wc
    001    已完成
    002    未生产
    003    生产中
      

  4.   

    create table #t(xh varchar(100), a int, b int, c int)
    insert into #t
    select '001',    3,    0,    4 union all
    select '002' ,   4 ,   1 ,   1  union all
    select '003'  ,  4  ,  2  ,  3
    select 
        xh,
        case when c>a then '已完成' when b+c>=a then '生产中' else '未生产' end
    from #t
    drop table #t
      

  5.   

    create table #t(xh varchar(100), a int, b int, c int)
    insert into #t
    select '001',    3,    0,    4 union all
    select '002' ,   4 ,   1 ,   1  union all
    select '003'  ,  4  ,  2  ,  3
    select 
        xh,
        case when c>a then '已完成' when b+c>=a then '生产中' else '未生产' end as wc
    from #t
    drop table #t