如何取得每天的第一个记录?

FlySpace 2005-01-22 12:51:34
有1个日志表,要求取得每天的第一个最后一个记录,请各位帮忙,谢了.
...全文
172 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
RainYang 2005-01-22
  • 打赏
  • 举报
回复
select min(id) from tbl where 日期=getdate()
631799 2005-01-22
  • 打赏
  • 举报
回复
--取得每天的第一个最后一个记录
select max(姓名),max(登录时间) from 表 group by convert(char(10),登录时间120)
Softlee81307 2005-01-22
  • 打赏
  • 举报
回复
-----------------下面是一段測試的 -----------
Create Table test(tname varchar(10) ,gtime datetime)
insert into test
select 'A','2005-01-01 18:00' union all
select 'A','2005-01-01 19:25' union all
select 'A','2005-01-01 07:08' union all
select 'B','2005-01-01 18:00' union all
select 'b','2005-01-01 19:25' union all
select 'B','2005-01-01 07:08'
-----------------------------------------------------

---------------------下面這段是取不同用戶的第一次和最後一次--------


select tname,第一次=min(gtime),最後一次=max(gtime) from test group by tname,convert(varchar(10),gtime,120)
-----------------------結果如下--------------------

A 2005-01-01 07:08:00.000 2005-01-01 19:25:00.000
B 2005-01-01 07:08:00.000 2005-01-01 19:25:00.000

------------------刪除測試--------------
drop table test
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
恩 好的
谢谢大家
结贴了 ...
Softlee81307 2005-01-22
  • 打赏
  • 举报
回复
試一下這條

select 用户,max(登录时间),min(登录时间)
from 表
group by 用户,convert(varchar(10),登录时间,120)
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
Softlee81307(孔腎) : 还要区分日期的啊
Softlee81307 2005-01-22
  • 打赏
  • 举报
回复
select 用户,max(登录时间),min(登录时间)
from 表
group by 用户,convert(varchar(10),登录时间,120)
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
获得datetime的日期方式用转换字符,慢阿,大家有好的方法吗
Softlee81307 2005-01-22
  • 打赏
  • 举报
回复
--------或者用下面的語句也可以隻用一條就行
-----------------下面是一段測試的 -----------
Create Table test(tname varchar(10) ,gtime datetime)
insert into test
select 'A','2005-01-01 18:00' union all
select 'A','2005-01-01 19:25' union all
select 'A','2005-01-01 07:08' union all
select 'B','2005-01-01 18:00' union all
select 'b','2005-01-01 19:25' union all
select 'B','2005-01-01 07:08'
-----------------------------------------------------

---------------------下面這段是取不同用戶的第一次和最後一次--------


select tname,第一次=min(gtime),最後一次=max(gtime) from test group by tname ---隻要這條就行
-----------------------結果如下--------------------

A 2005-01-01 07:08:00.000 2005-01-01 19:25:00.000
B 2005-01-01 07:08:00.000 2005-01-01 19:25:00.000

------------------刪除測試--------------
drop table test
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
基本解决了,谢谢大家帮助
我的方法是:
select 用户,max(登录时间),min(登录时间)
from 表
where datediff(Day,convert(datetime,substring(convert(varchar,登录时间),1,10)),登录时间)<1
group by 用户,substring(convert(varchar,登录时间))

Softlee81307 2005-01-22
  • 打赏
  • 举报
回复
-----------------下面是一段測試的 -----------
Create Table test(tname varchar(10) ,gtime datetime)
insert into test
select 'A','2005-01-01 18:00' union all
select 'A','2005-01-01 19:25' union all
select 'A','2005-01-01 07:08' union all
select 'B','2005-01-01 18:00' union all
select 'b','2005-01-01 19:25' union all
select 'B','2005-01-01 07:08'
-----------------------------------------------------

---------------------下面這段是取不同用戶的第一次和最後一次--------
select * from
( select tname,gtime=min(gtime) from test group by tname
union
select tname,gtime=max(gtime) from test group by tname) a order by tname,gtime

-----------------------結果如下--------------------
A 2005-01-01 07:08:00.000
A 2005-01-01 19:25:00.000
B 2005-01-01 07:08:00.000
B 2005-01-01 19:25:00.000

------------------刪除測試--------------
drop table test
RainYang 2005-01-22
  • 打赏
  • 举报
回复
select * from
(select min(姓名) as name, min(登录时间) as 登录时间 from test group by 登录时间,姓名
union all
select max(姓名) as name, max(登录时间) as 登录时间 from test group by 登录时间,name) as a order by 姓名,日期
didoleo 2005-01-22
  • 打赏
  • 举报
回复
-排序方式改一点
select * from #ttt a
where not exists (select 1 from #ttt where 姓名=a.姓名 and 登录时间<a.登录时间)
union all

select * from #ttt a
where not exists (select 1 from #ttt where 姓名=a.姓名 and 登录时间>a.登录时间)

order by 姓名,登录时间
didoleo 2005-01-22
  • 打赏
  • 举报
回复
create table #ttt
(
姓名 varchar(10),
登录时间 datetime
)

insert into #ttt
select 'A' , '2005-01-01 18:00' union all
select 'A' , '2005-01-01 19:25' union all
select 'A' , '2005-01-01 07:08'




select * from #ttt a
where not exists (select 1 from #ttt where 姓名=a.姓名 and 登录时间<a.登录时间)
union all

select * from #ttt a
where not exists (select 1 from #ttt where 姓名=a.姓名 and 登录时间>a.登录时间)

order by 登录时间

-------------------------------
A 2005-01-01 07:08:00.000
A 2005-01-01 19:25:00.000


(所影响的行数为 2 行)
wang_jzh 2005-01-22
  • 打赏
  • 举报
回复
可以
select top 1 * from tbl group by 用户,covert('114',日期) order by 日期 desc
RainYang 2005-01-22
  • 打赏
  • 举报
回复
可以
select max(姓名), max(登录时间) from 表 group by 姓名,登录时间
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
可以按用户和时间group吗,得到不同用户的第1次和最后1次
wang_jzh 2005-01-22
  • 打赏
  • 举报
回复
select top 1 * from tbl where 日期=getdate() order by 日期 desc 最后登陆时间
FlySpace 2005-01-22
  • 打赏
  • 举报
回复
try...
meilian01 2005-01-22
  • 打赏
  • 举报
回复
该死

select max(姓名), max(登录时间) from 表 group by 登录时间
加载更多回复(7)

34,838

社区成员

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

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