一统计的sql问题,大家来看看
现在有一个表A
id otherid etc time
1 33 abcd ..
2 33 wdct ..
3 34 qwct ..
4 54 5675
.. .. ..
现在要统计有多少个otherid, 多少个三位为c的etc
要求otherid唯一,下面的语句能统计唯一的otherid
但是etc的统计就没办法distinct otherid了,求解
select count(distinct otherid),
sum(
case substring(isanythingm,3,1)
when 'c ' then 1
else 0
end
)
where 1=1
group by month(time)
------解决方案--------------------try:
select count(distinct otherid),
count( distinct
case substring(isanythingm,3,1)
when 'c ' then otherid
else null
end
)
from ...
group by month(time)
------解决方案--------------------select count(distinct otherid) from
(
select * from tb where substring(etc,3,1) = 'c '
) t
------解决方案-------------------- select count(distinct otherid),sum(case thingm when 'c ' then 1 else 0 end )
from
(select distinct otherid,substring(isanythingm,3,1) as thingm,time
from table
where 1=1) a
group by month(time)
这样试试,先把 字符截取好 再GROUP
------解决方案--------------------create table test(id int,otherid int,etc varchar(10))
insert test select 1,33, 'abcd '
union all select 2,33, 'wdct '
union all select 3,34, 'qwct '
union all select 4,54, '5675 '
select count(1) from test a where id=
(
select top 1 id from test where otherid=a.otherid and substring(etc,3,1)= 'c '
)
-----------
2