不太明白你的意思,我理解的好像是你case错了,你可以case id='01' then 1 else NULL end case

解决方案 »

  1.   

    我的意思是如果photo里面等于1或2的话
    就把该id记入总数(不要相同的)
    不能用z5wjz(z5wjz) 的case id='01'...是有其他方法可以(先把所有photo等于1或2的id要出来,distinct,在count),但是我就是想问问能不能在case when 的情况里面在加上类似distinct的功能
      

  2.   

    这样试试:
    count(distinct (case when ...) )
      

  3.   

    count(distinct case when photo='1' or photo='2' then 1 else null end )PHnumber这样就可以用了
      

  4.   

    如果是“photo里面等于1或2的话就把该id记入总数(不要相同的)”,应该是在where条件中对photo做限制,count时是count(distinct id)
      

  5.   

    to: duanzilin(寻) sasacat(傻傻猫) 不能这样查的,这样结果是都是1
    to:bobfang(匆匆过客) 我也知道这种方法,但是我要count(case when...)这种模式下用distinct
    难道这的没法用吗?
      

  6.   

    你自己的语句查询出来不是
    ID         NAME         PHNUMBER
    ---------- ---------- ----------
    01         aaa                 2
    02         bbb                 1
    03         ccc                 0吗?你想要什么样的结果集啊?写个完整一点的看看啊
      

  7.   

    count(distinct case when photo='1' or photo='2' then id else null end )PHnumber就可以了
      

  8.   

    to: sasacat(傻傻猫) 不是这个结果
    我要的count结果是2条
    01和02
    to: nebulaly 不能这样用distinct,结果为1
      

  9.   

    select ID ,NAME, count(case when photo=1 or photo=2 then 1 else null end)PHnumber
    from T_LOGIN_LOG 
    where photo in(1,2)
     group by ID ,NAME这样?ID         NAME         PHNUMBER
    ---------- ---------- ----------
    01         aaa                 2
    02         bbb                 1
      

  10.   

    to sasacat(傻傻猫) 谢谢你,这样可以
    不过对不起
    我之前没说清楚
    我是想仅仅要count,不用把ID,NAME查出来,我是为了group by 用的(按照id第一位0/1/2...分组)
    select substr(id,1,1)
           ,count(case when photo=1 or photo=2 then 1 else null end) PHnumber
           ,count(case when photo=3 then 1 else null end) PJnumber
    from table
    group by substr(id,1,1)
    这样的结果没有distinct是可以这样分开来查
    ..
    select substr(id,1,1)
           count(distinct(id))
    from table where photo=1 or photo=2 
    group by substr(id,1,1)
    ...
    select substr(id,1,1)
           count(distinct(id))
    from table where photo=3
    group by substr(id,1,1)
    ...
    再把结果合起来
    不过速度应该会慢
    所以我问大家能不能直接在count(case when )状态下用distinct的类似功能
      

  11.   

    count(distinct case when photo='1' or photo='2' then id else null end )PHnumberthen 1 作count结果是1then id应该就不是1了
      

  12.   

    谢谢 nebulaly(极高明而道中庸) 
    之前没注意到你写的id
    count(distinct case when photo='1' or photo='2' then id else null end )PHnumber
    可以
    揭帖