什么数据库啊?不同的数据库写法不一样的
oracle:
select cid,
sum(decode(,'优',1,0)) 优,
sum(decode(,'良',1,0)) 良
from mytable
group by cid;sql server:
select cid,
sum(case when ='优' then 1 else 0 end case) 优,
sum(case when ='良' then 1 else 0 end case) 良
from mytable
group by cid;
oracle:
select cid,
sum(decode(,'优',1,0)) 优,
sum(decode(,'良',1,0)) 良
from mytable
group by cid;sql server:
select cid,
sum(case when ='优' then 1 else 0 end case) 优,
sum(case when ='良' then 1 else 0 end case) 良
from mytable
group by cid;
select cid,
sum(iif( ='优' ,1,0)) as 优,
sum(iif( ='良' ,1,0)) as 良,
from mytable
group by cid
select cid,
sum(case when ='优' then 1 else 0) as 优,
sum(case when ='良' then 1 else 0) as 良,
...
sum(case when ='中' then 1 else 0) as 中
from mytable
group by cid
Access
select cid,
sum(iif( ='优' ,1,0)) as 优,
sum(iif( ='良' ,1,0)) as 良 -----erickleung() 的这里多了逗号
from mytable
group by cid
1.这种最简单:
select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
from tname group by cid;2.这个也可以:
select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
select cid,,count(*) cnt
from tname
group by cid,,
1.这种最简单:
select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
from tname group by cid;2.这个也可以:
select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
from (
select cid,,count(*) cnt
from tname
group by cid,,
)
1.这种最简单:
select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
from tname group by cid;2.这个也可以:
select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
from(
select cid,,count(*) cnt
from tname
group by cid,,
)
select cid,sum(iif( ='优' ,1,0)) as 优,sum(iif( ='良' ,1,0)) as 良 from tablename group by cid
sum(case when ='优' then 1 else 0) as 优,
sum(case when ='良' then 1 else 0) as 良,
...
sum(case when ='中' then 1 else 0) as 中
from mytable
group by cid