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
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的类似功能
count(distinct case when photo='1' or photo='2' then id else null end )PHnumberthen 1 作count结果是1then id应该就不是1了
谢谢 nebulaly(极高明而道中庸) 之前没注意到你写的id count(distinct case when photo='1' or photo='2' then id else null end )PHnumber 可以 揭帖
就把该id记入总数(不要相同的)
不能用z5wjz(z5wjz) 的case id='01'...是有其他方法可以(先把所有photo等于1或2的id要出来,distinct,在count),但是我就是想问问能不能在case when 的情况里面在加上类似distinct的功能
count(distinct (case when ...) )
to:bobfang(匆匆过客) 我也知道这种方法,但是我要count(case when...)这种模式下用distinct
难道这的没法用吗?
ID NAME PHNUMBER
---------- ---------- ----------
01 aaa 2
02 bbb 1
03 ccc 0吗?你想要什么样的结果集啊?写个完整一点的看看啊
我要的count结果是2条
01和02
to: nebulaly 不能这样用distinct,结果为1
from T_LOGIN_LOG
where photo in(1,2)
group by ID ,NAME这样?ID NAME PHNUMBER
---------- ---------- ----------
01 aaa 2
02 bbb 1
不过对不起
我之前没说清楚
我是想仅仅要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的类似功能
之前没注意到你写的id
count(distinct case when photo='1' or photo='2' then id else null end )PHnumber
可以
揭帖