表 t,
结构: id,no,meno
数据:
1,345,-1
2,678,1
3,789,1统计no的个数
1 就加1
-1就减去1我用count(case when meno='1' then 1 when meno='-1' then -1 end )
这样为什么统计不出正确的结果?

解决方案 »

  1.   

    靠,应该是
    count(no)+sum(meno)才对
      

  2.   

    改用sum就可以了。count不管你是 count(1)还是count(-1)计数都会加的,null将忽略。
      

  3.   

    count--改sum
    不太明白,是不是sum(no*meno)
      

  4.   

    改成sum就行了.
    sum是累加
    count只是统计
      

  5.   


    sum(case when meno='1' then 1 when meno='-1' then -1 end )
      

  6.   

    declare @t table (id int ,no int,meno int)
    insert @t select 1,345,-1
    insert @t select 2,678,1
    insert @t select 3,789,1
    insert @t select 3,789,-1
    select sum(meno) as [Count],no from @t group by no 
      

  7.   


    declare  @AA table(id int,no varchar(50),meno int)insert into @AA(id ,no ,meno)
    select 1,'345',-1 union all
    select 2,'678',1 union all
    select 3,'789',1 select id,no,
    Count=sum(case when meno='1' then no+1 else no-1 end ) 
    from @aa 
    group by id,no
      

  8.   


    我用count(case when meno='1' then 1 when meno='-1' then -1 end )
    这样为什么统计不出正确的结果?
    case when meno='1' then count(id) else 0 end
    case when men0 = '-1' then count(id) else 0 end)
      

  9.   


    if object_Id ('表t') is not null
       drop table 表t 
    go
    create table 表t
    ( id int,
      no int,
      meno int
    )
    insert into 表t select 1,345,-1
          union all select 2,678,1
          union all select 3,789,1
    go
    select id ,no=case when meno=-1 then no-1 else no+1 end ,meno from 表t
      

  10.   

    用sum聚合函数,count函数只是数数而已,不管你记录是怎么样的,sum求和就能进行加减了,能够正确反映问题.