日期:2014-05-19  浏览次数:20539 次

请问如何将记录按日期统计个数?
现有
表A
CID         DATE                      
001         2007-1-1              
002         2007-1-1              
002         2007-1-2            
003         2007-1-1              
003         2007-1-1            
003         2007-1-2            


要求按日期统计CID个数
生成下表:
表B

CID       COUNT         DATE
001       1               2007-1-1
002       1               2007-1-1  
003       2               2007-1-1  
002       1               2007-1-2
003       1               2007-1-2

请各位大侠帮帮忙!



------解决方案--------------------
select CID,count(1) as [COUNT],DATE from A group by CID,DATE
------解决方案--------------------
create table tab1
(
CID varchar(10),
DATE datetime
)
insert into tab1 select '001 ', '2007-1-1 '
insert into tab1 select '002 ', '2007-1-2 '
insert into tab1 select '002 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-2 '


select cid , count(1) , convert(char(10),[date],21) [date]
from tab1
group by cid , [date]
------解决方案--------------------
按日期统计CID个数
select cid , count(*) , convert(char(10),[date],120)
from 表A
group by cid,convert(char(10),[date],120) order by convert(char(10),[date],120)

------解决方案--------------------
create table tab1
(
CID varchar(10),
DATE char(20)
)
insert into tab1 select '001 ', '2007-1-1 '
insert into tab1 select '002 ', '2007-1-2 '
insert into tab1 select '002 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-1 '
insert into tab1 select '003 ', '2007-1-2 '
select CID,count(1) as [COUNT],DATE from tab1 group by CID,DATE
drop table tab1
result:
CID COUNT DATE
---------- ----------- --------------------
001 1 2007-1-1
002 1 2007-1-1
003 2 2007-1-1
002 1 2007-1-2
003 1 2007-1-2

(所影响的行数为 5 行)