有一个客户日志表。包括客户cid,信息等级clevel(1,2,两个等级),信息状态cstatus (0,1两种状态)现在要求获取客户cid='111'统计信息,统计条件分别是的clevel=1,cstatus等于0和cstatus等于1的客户日志总数结果大概如下:客户cid     等级为1   状态为0  状态为1    111          12000    1000    500还有此表数据量比较大,谢谢拉

解决方案 »

  1.   

    select cid as [客户cid], 
    sum(case when clevel=1 then 1 esle 0 end) as [等级为1],
    sum(case when cstatus=0 then 1 else 0 end) as [状态为0],
    sum(case when cstatus=1 then 1 esle 0 end) as [状态为1]
    from 客户日志表
    group by cid
      

  2.   

    select cid , 
    sum(if(clevel=1,1,0)) , 
    sum(if(cstatus=0,1,0)),
    sum(if(cstatus=1,1,0)) from tt where cid='111' group by cid 
      

  3.   

    不是用count么,怎么用sum了啊
      

  4.   

    一条记录满足条件为1,否则为0,分组求和,与COUNT一样的
      

  5.   

    我给来个用count实现的。select cid as '客户cid' , (select count(*) from 客户日志表 where cid='111' and clevel=1) as '等级为1', (select count(*) from 客户日志表 where cid='111' and cstatus=1) as '状态为0',(select count(*) from 客户日志表 where cid='111' and cstatus=1) as '状态为1' from 客户日志表 where cid='111' limit 1;
      

  6.   

    不过,建议楼主以从学习的角度用,还是学学 wwwwb 的代码,对你以后有好处。
    虽然这样写效率很低,但是有些方面想用一条sql语句出来,还就得按照这个模子套。
      

  7.   

    非常感谢各位,看来我得好好看看sql基础知识了