表: SFD_2 ( ZYH, SFLB, SJFY)要求输出:按 ZYH 分组,在同一行里分不同的 SFLB 求 SJFY  的和语句:select ZYH ,sum(SJFY),sum(case when SFLB='700001' then SJFY else 0 end ) ,
         sum(SJFY),sum(case when SFLB='700002' then SJFY else 0 end ) 
  from SFD_2 
  where ZYH>='95001'
  group by ZYH结果 ,类似  sum(case when SFLB='700001' then SJFY else 0 end )  没有求和,永远为 0
应该怎样实现?

解决方案 »

  1.   


    是说:类似  sum(case when SFLB='700001' then SJFY else 0 end )  没有实现求和作用,,应该怎样写,才能按不同的 SFLB 来求和 
      

  2.   

    select ZYH ,sum(case when SFLB='700001' then SJFY else 0 end ) 
             ,sum(case when SFLB='700002' then SJFY else 0 end ) 
      from SFD_2 
      where ZYH>='95001'
      group by ZYH
    这样不行吗
      

  3.   

    目测语句没问题
      select * from SFD_2
      where zyh>='95001' and sflb in('700001','700002')
    这个看有木有数据出来
      

  4.   

    zyh 的数据类型是 字符型的吧?
    -- 这两个条件的原因
    SFLB='700001' 
    zyh>='95001' 
      

  5.   


     sum(case when SFLB='700001' then convert(int,SJFY) else 0 end ) 转int求和