日期:2014-05-17  浏览次数:20451 次

请教分组里分组并统计次数,都不知应叫这什么分组 sql2000
表,请用sql2000,另外这叫什么分组,我都不知应叫这什么

ID1       ID2      YESNO
E00001    001      0
E00001    001      0
E00001    001      1
E00002    001      0
E00002    001      1
E00002    001      1
E00002    001      1
E00002    002      0
E00002    002      0
E00003    002      1
E00003    002      0

统计结果

ID2     ID1      YES(0) 分组统计,这里不知如何描述(ID1,ID2)
001     E00001   2次     3次
001     E00002   1次     4次
002     E00002   2次     2次
002     E00003   1次     2次  

谢谢先!

------解决方案--------------------

create table sd
(ID1 varchar(10),ID2 varchar(10),YESNO int)

insert into sd
 select 'E00001','001',0 union all
 select 'E00001','001',0 union all
 select 'E00001','001',1 union all
 select 'E00002','001',0 union all
 select 'E00002','001',1 union all
 select 'E00002','001',1 union all
 select 'E00002','001',1 union all
 select 'E00002','002',0 union all
 select 'E00002','002',0 union all
 select 'E00003','002',1 union all
 select 'E00003','002',0


select a.ID2,
       a.ID1,
       rtrim(sum(case YESNO when 0 then 1 else 0 end))+'次' 'YES(0)',
       rtrim(count(1))+'次' '分组统计'
 from sd a
 group by a.ID2,a.ID1

/*
ID2        ID1        YES(0)         分组统计
---------- ---------- -------------- --------------
001        E00001     2次             3次
001        E00002     1次             4次
002 &