sql2000 互相比较问题 谢谢
表1中本次和上次比较,如果本次大则显示1,如果本次小则显示0表1
id 上次 本次
1   3    5
2   1    2
3   4    1
4   3    2
得表2id 上次 本次  比较
1   3    5     1
2   1    2     1
3   4    1     0
4   3    2     0

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[上次] int,[本次] int)
    insert [tb]
    select 1,3,5 union all
    select 2,1,2 union all
    select 3,4,1 union all
    select 4,3,2
    goselect *,比较=case when 本次>上次 then 1 else 0 end from tb/**
    id          上次          本次          比较
    ----------- ----------- ----------- -----------
    1           3           5           1
    2           1           2           1
    3           4           1           0
    4           3           2           0(4 行受影响)
    **/
      

  2.   

    create table tb1(id int,上次 int,本次 int)
    insert into tb1 select 1,3,5
    insert into tb1 select 2,1,2
    insert into tb1 select 3,4,1
    insert into tb1 select 4,3,2
    go
    select *,(sign(上次-本次)+1)/2 比较 from tb1
    go
    drop table tb1
    /*
    id          上次          本次          比较
    ----------- ----------- ----------- -----------
    1           3           5           0
    2           1           2           0
    3           4           1           1
    4           3           2           1(4 行受影响)
    */
      

  3.   

    反了...
    create table tb1(id int,上次 int,本次 int)
    insert into tb1 select 1,3,5
    insert into tb1 select 2,1,2
    insert into tb1 select 3,4,1
    insert into tb1 select 4,3,2
    go
    select *,(sign(本次-上次)+1)/2 比较 from tb1
    go
    drop table tb1
    /*
    id          上次          本次          比较
    ----------- ----------- ----------- -----------
    1           3           5           1
    2           1           2           1
    3           4           1           0
    4           3           2           0(4 行受影响)
    */
      

  4.   


    declare @表1 table (id int,上次 int,本次 int)
    insert into @表1
    select 1,3,5 union all
    select 2,1,2 union all
    select 3,4,1 union all
    select 4,3,2select *,比较=(sign(本次-上次)+2)/2 from @表1
    /*
    id          上次          本次          比较
    ----------- ----------- ----------- -----------
    1           3           5           1
    2           1           2           1
    3           4           1           0
    4           3           2           0
    */
      

  5.   

    select *,case when 本次>上次 then 1 else 0 end as 比较 from tb
    --另外补充一下,楼主未说明相等时的算法,上面的算法当相等时设为0
      

  6.   

    select *,比较=case when 本次>上次 then 1 else 0 end from tb
      

  7.   

    select *,比较=case when 本次>上次 then 1 else 0 end from tb