急求SQL语句!!!

姑娘出来我爬山坡 2013-06-26 09:59:29
表:用户id,桌子id,登录状态,登录时间,退出时间

一张桌子上有可能有多个用户登录,每个用户的最后一条记录为当前登录状态记录。

找出某个时间段,该桌子上用户的最后一条登录状态记录,如果有用户在这个时间段没有记录,那么不查出来。




...全文
146 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 8 楼 Imaor 的回复:

--时间段 starttime,endtime
--tableid 具体id你自己定
declare @tableid int
set @tableid=1

---查所有 符合时间段、桌子id条件的记录
select * from t_record 
where tableid=@tableid 
and (startTime between loginTime and logoutTime)
and (endTime between loginTime and logoutTime)

--查最后一个登陆的
select top 1 * from t_record 
where tableid=@tableid 
and (startTime between loginTime and logoutTime)
and (endTime between loginTime and logoutTime)
order by loginTime desc

不好意思。 你好像弄错意思了。 我想要的是 该桌子上的所有用户在定义的时间段中的最后一条记录,并且最后一条记录的登录状态为离线,在线的不用查出来。 如果该桌上有2个用户在时间段中最后一条记录都为离线,那么就都查出来。如果只有1个用户就查1条。 没有更优的话 回复我一下。 我给你们结贴。
IEEE_China 2013-06-26
  • 打赏
  • 举报
回复

--时间段 starttime,endtime
--tableid 具体id你自己定
declare @tableid int
set @tableid=1

---查所有 符合时间段、桌子id条件的记录
select * from t_record 
where tableid=@tableid 
and (startTime between loginTime and logoutTime)
and (endTime between loginTime and logoutTime)

--查最后一个登陆的
select top 1 * from t_record 
where tableid=@tableid 
and (startTime between loginTime and logoutTime)
and (endTime between loginTime and logoutTime)
order by loginTime desc

  • 打赏
  • 举报
回复
select t from t_record t where t.tableId = tableId and t.logoutTime > startTime and t.logoutTime < endTime and not exit (select s from t_record s where s.loginTime >= t.logoutTime and t.tableId = tableId ) 这个是我刚弄出来的。 有没有更优的方案,startTime-endTime 是定义的时间段
IEEE_China 2013-06-26
  • 打赏
  • 举报
回复

--查最后一条登录数据
select top 1 * from #temp 
where tableid=@tableid and (@querytime between logintime and quittime)
order by 1 desc
叶子 2013-06-26
  • 打赏
  • 举报
回复
select * from tablename t 
where dtime=(select max(dtime) from tablename where userid=t.userid)
and dtime between 开始时间 and 结束时间
IEEE_China 2013-06-26
  • 打赏
  • 举报
回复


if object_id('Tempdb..#temp') is not null drop table #temp
--建临时表
create table #temp(
Id int identity(1,1) not null,
userid int null,
tableid int null,
loginstatus int null,
logintime datetime null,
quittime datetime null
)
--测试用例
Insert #temp 
select 1,1,1,'2013-06-21 01:00:00','2013-06-21 05:00:00' union all
select 2,1,1,'2013-06-21 01:20:00','2013-06-21 01:25:00' union all
select 3,1,1,'2013-06-21 01:30:00','2013-06-21 01:40:00' union all
select 1,1,1,'2013-06-22 02:00:00','2013-06-22 02:30:00' union all
select 2,1,1,'2013-06-22 02:20:00','2013-06-22 02:45:00' union all
select 3,1,1,'2013-06-22 02:10:00','2013-06-22 03:20:00' union all
select 1,1,1,'2013-06-23 03:00:00','2013-06-23 03:10:00' union all
select 2,1,1,'2013-06-23 03:20:00','2013-06-23 04:55:00' union all
select 3,1,1,'2013-06-23 03:30:00','2013-06-23 03:40:00'

--定义 查询时间、桌子id 变量
declare @querytime datetime
declare @tableid int

set @querytime='2013-06-22 03:10:00'
set @tableid=1

--根据你的条件查询
--本例查询 桌子id为1 、时间为 2013-06-22 03:10:00 的登录记录
select * from #temp 
where tableid=@tableid and (@querytime between logintime and quittime)

--查询结果


(9 行受影响)
Id          userid      tableid     loginstatus logintime               quittime
----------- ----------- ----------- ----------- ----------------------- -----------------------
6           3           1           1           2013-06-22 02:10:00.000 2013-06-22 03:20:00.000
xiaoqi7456 2013-06-26
  • 打赏
  • 举报
回复
引用 2 楼 u011206051 的回复:
[quote=引用 1 楼 xiaoqi7456 的回复:] 1、测试数据 2、业务逻辑 3、想要的结果
我想要SQL语句。。[/quote] 。。。我是要你根据这个格式提问题, 你讲一堆中文逻辑,别人怎么写语句给你?
  • 打赏
  • 举报
回复
引用 1 楼 xiaoqi7456 的回复:
1、测试数据 2、业务逻辑 3、想要的结果
我想要SQL语句。。
xiaoqi7456 2013-06-26
  • 打赏
  • 举报
回复
1、测试数据 2、业务逻辑 3、想要的结果

22,209

社区成员

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

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