select 列1 ,列2 , sum(case 列4 when 'true' then 列3 else -列3 end) '数量' from yourtable 
group by 列1,列2 order by 列1,列2

解决方案 »

  1.   

    select 列1 ,列2 , sum(case 列4 when 'false' then -列3 else 列3 end) from yourtable 
    group by 列1,列2 order by 列1,列2
      

  2.   

    厉害!
    我平时很少用到case,又学到东西了,呵呵
      

  3.   

    select 列1 ,列2 , sum(case when 列4='false' then -列3 else 列3 end) from yourtable 
    group by 列1,列2 order by 列1,列2
      

  4.   

    如果我要是想去掉相减后得0的数值是不是这样写?select 列1 ,列2 , sum(case when 列4='false' then -列3 else 列3 end) as abc
    from yourtable 
    where abc <> 0 
    group by 列1,列2 
    order by 列1,列2有错误
    我该怎么改?
      

  5.   

    select 列1 ,列2 , 
        sum(insull(列3,0) * (case when 列4='ture' then 1 else -1 end)) ABC
    from yourtable 
    group by 列1,列2 
    having sum(列3 * (case when 列4='ture' then 1 else -1 end)) <> 0
    order by 列1,列2
      

  6.   

    select 列1 ,列2 , 
        sum(isnull(列3,0) * (case when 列4='ture' then 1 else -1 end)) ABC
    from yourtable 
    group by 列1,列2 
    having sum(列3 * (case when 列4='ture' then 1 else -1 end)) <> 0
    order by 列1,列2--insull----> isnull