select 
sum(实时话费),count(*) from (
select a.user_id,
sum(case when a.kind='zdjm' then a.je else 0 end) 帐单减免,
sum(case when a.kind='hbkz' then a.je else 0 end) 划拨扣帐,
sum(case when a.kind='shqf' then a.je else 0 end) 收回欠费,
/*sum(case when a.kind='zhyc' then a.je else 0 end) 帐户级预存,
sum(case when a.kind='yhyc' then a.je else 0 end) 用户级预存,*/
sum(case when a.kind='sshf' then a.je else 0 end) 实时话费,
sum(case when a.kind='gchf' then a.je else 0 end) 估测话费,
sum(case when a.kind='tcdx' then a.je else 0 end) 套餐低消,
sum(case when a.kind='yhdx' then a.je else 0 end) 用户低消,
sum(case when a.kind='yhzjdq' then a.je else 0 end) 用户租机到期,
sum(case when a.kind='zhzjdq' then a.je else 0 end) 帐户租机到期,
sum(case when a.kind='yhzhdx' then a.je else 0 end) 用户帐户低消,
sum(case when a.kind='zhdx' then a.je else 0 end) 帐户低消,
sum(case when a.kind='tf' then a.je else 0 end) 退费
from dsz_kzyc_new a
group by a.user_id having sum(case when a.kind='sshf' then a.je else 0 end)<>0)这个脚本为什么注释两行跟不注释结果不一样。谢谢

解决方案 »

  1.   

    a.user_id 有多少记录? 还有注释的两条会不会为0
      

  2.   

    当然一样了。 你注释的是两列的结果,又不是两行数据。SUM 和 COUNT 不受影响
      

  3.   

    shadow1986a  好像没有看清人家的意思
      

  4.   

    注意count的用法:
    COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function.If you specify DISTINCT, then you can specify only the query_partition_clause of the analytic_clause. The order_by_clause and windowing_clause are not allowed.If you specify expr, then COUNT returns the number of rows where expr is not null. You can count either all rows, or only distinct values of expr.If you specify the asterisk (*), then this function returns all rows, including duplicates and nulls. COUNT never returns null.Maybe return 0.
      

  5.   

    count(*) 我只是为了说条数,确认条数是不一样了,合计数也不一样了,我的记录近200万条,不会是因为条数多的原因吧,我别的表可是上千万条记录呢,也没有这个问题。我觉得很灵异。
      

  6.   


    那2个类型的条数是不是 411820 - 406764 ?里面计算的行数 只是包涵里面列出kind=这些字符串的行数 没列出来的其他字符串自然不计算在里面了