求一组SQL(有难度)

mouse2003 2003-08-25 02:35:53
有一个表格记录每天的登陆情况,字段是用户名userid及登陆时间logontime。
我需要从这个表中统计出:
1.今天与昨天相比新增些了哪些登录用户?
2.连续一周每天登陆的用户是哪些?
3.最近一周从未登陆的用户有哪些?

请问高手这些功能该如果使用SQL实现?谢谢!
...全文
38 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-08-25
  • 打赏
  • 举报
回复
1.今天与昨天相比新增些了哪些登录用户?

select * from 表 where datediff(day,logontime,getdate())=0 and userid not in (select userid from 表 where datediff(day,logontime,getdate())=1)

2.连续一周每天登陆的用户是哪些?

select userid from 表 where datediff(day,logontime,getdate())<=7 group by userid having count(distinct CONVERT(varchar(10),logontime,120))=7

3.最近一周从未登陆的用户有哪些?

select * from 用户表 where userid not in (select userid from 表 where datediff(day,logontime,getdate())<=7)
CrazyFor 2003-08-25
  • 打赏
  • 举报
回复
1,
select * from 表 aa left join 表 bb on aa.userid=bb.userid
where convert(char(10),aa.登陆时间,120)=convert(char(10),getdate(),120) and convert(char(10),bb.登陆时间,120)=convert(char(10),getdate()-1,120) and bb.userid is null

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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