34,576
社区成员
发帖
与我相关
我的任务
分享
if object_id('[login]') is not null drop table [login]
go
create table [login]([ID] int,[time] datetime,[Islogin] int,[UserID] int)
insert [login]
select 1,'2010-04-01',1,223 union all
select 2,'2010-05-02',1,223 union all
select 3,'2010-05-03',0,223 union all
select 4,'2010-05-04',1,223 union all
select 5,'2010-05-05',0,223 union all
select 6,'2010-06-01',0,223 union all
select 7,'2010-06-02',1,223 union all
select 8,'2010-06-03',1,223
go
select convert(varchar(7),[time],120) as 月份,count(1) as 登录次数
from login
where userid=223 and islogin=1
group by convert(varchar(7),[time],120)
/**
月份 登录次数
------- -----------
2010-04 1
2010-05 2
2010-06 2
(3 行受影响)
**/
select convert(varchar(7),[time],120) as 月份,count(1) as 登录次数
from login
where userid=223 and islogin=1
group by convert(varchar(7),[time],120)