中国教程网《Photoshop专家讲堂》光盘热售参与论坛活动,快速赚取金币精品素材,中英文字体
发新话题
打印

sql语句的问题

sql语句的问题


数据库中记录如下  
023   1  
023   2  
024   1  
023   3  
023   4  
024   2  
023   1  
..........  
现在需要的目的是  
id            是1的总数    不是1的总数  
023    3        3  
024    2        0  
.......................  

在线等,解决问题马上结账  
---------------------------------------------------------------  

select  id,sum(case  条件=1  then  1  else  0  end),sum(case  条件<>1  then  1  else  0  end)  from  table  group  by  id  
---------------------------------------------------------------  

select  id,  
sum(case  when  dd='1'  then  1  else  0  end)as  是1的总数,  
sum(case  when  dd<>'1'  then  1  else  0  end)as  不是1的总数  
from  tbname  
group  by  id  
---------------------------------------------------------------  

Select  D.id,D.yes,D.no  from(  
SELECT  C.id,A.YES,B.NO  FROM  Table  C,  
(select  count(*)  as  yes  from  table  where  id='A00001')  A,  
(select  count(*)  as  no  from  talble  where  id<>'A00001')  B  
)  D  
---------------------------------------------------------------  

select  id,sum(case  when........end),sum(case  when  ......end)from  table  group  by  id

TOP

发新话题