SQL group by 的问题

呵呵112123 2010-05-31 12:38:15
表:login
字段,ID自增 , time 时间, Islogin bool, UserID 用户ID。
1 2010-04-01 1 223
2 2010-05-02 1 223
3 2010-05-03 0 223
4 2010-05-04 1 223
5 2010-05-05 0 223
6 2010-06-01 0 223
7 2010-06-02 1 223
8 2010-06-03 1 223

查询 223这个ID 一个月登陆了几次
该怎么写?SQL2005
...全文
36 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
呵呵112123 2010-05-31
  • 打赏
  • 举报
回复
如果是查天的的话是不是 group by convert(varchar(10),[time],120)??
呵呵112123 2010-05-31
  • 打赏
  • 举报
回复
网上找了个错的 半天没弄出来
htl258_Tony 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 goodlivelife 的回复:]
SQL code
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)
[/Quote]正解
GOODlivelife 2010-05-31
  • 打赏
  • 举报
回复
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 行受影响)
**/
GOODlivelife 2010-05-31
  • 打赏
  • 举报
回复
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)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧