时间问题的处理

ballatong 2019-07-02 09:20:17
请教,需要按userID汇总最后一列的合计时间,用时分秒表示,(最后一列目前是前两列时间的差)

userID stopstarttime stopendtime (No column name)
1 2019-07-01 14:49:55.130 2019-07-01 14:50:37.903 1900-01-01 00:00:42.773
1 2019-07-01 14:50:42.717 2019-07-01 14:52:07.670 1900-01-01 00:01:24.953
1 2019-07-01 14:52:17.160 2019-07-01 14:52:19.880 1900-01-01 00:00:02.720
1 2019-07-01 14:52:23.523 2019-07-01 15:01:13.510 1900-01-01 00:08:49.987
1 2019-07-01 14:28:41.997 2019-07-01 14:28:48.410 1900-01-01 00:00:06.413
1 2019-07-01 14:29:00.307 2019-07-01 14:49:43.390 1900-01-01 00:20:43.083
2 2019-07-01 14:29:55.470 2019-07-01 14:31:14.657 1900-01-01 00:01:19.187
2 2019-07-01 14:31:59.673 2019-07-01 14:35:00.807 1900-01-01 00:03:01.133
2 2019-07-01 14:35:32.280 2019-07-01 14:41:14.110 1900-01-01 00:05:41.830
...全文
203 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 版主 2019-07-05
  • 打赏
  • 举报
回复
引用 11 楼 吃货乙 的回复:
[quote=引用 10 楼 二月十六 的回复:] sql区惊现大Y
2月最近忙啥呢 都没看出来说话[/quote] 一直在加班
吃货乙 2019-07-05
  • 打赏
  • 举报
回复
引用 10 楼 二月十六 的回复:
[quote=引用 9 楼 吃货乙 的回复:] create table #t(userID int,stopstarttime datetime,stopendtime datetime,) insert into #t select 1,'2019-07-01 14:49:55.130','2019-07-01 14:50:37.903' union all select 1,'2019-07-01 14:50:42.717','2019-07-01 14:52:07.670' union all select 1,'2019-07-01 14:52:17.160','2019-07-01 14:52:19.880' union all select 1,'2019-07-01 14:52:23.523','2019-07-01 15:01:13.510' union all select 1,'2019-07-01 14:28:41.997','2019-07-01 14:28:48.410' union all select 1,'2019-07-01 14:29:00.307','2019-07-01 14:49:43.390' union all select 2,'2019-07-01 14:29:55.470','2019-07-01 14:31:14.657' union all select 2,'2019-07-01 14:31:59.673','2019-07-01 14:35:00.807' union all select 2,'2019-06-01 14:35:32.280','2019-07-01 14:41:14.110' -- 此处特意写06/01,以便测试小时计数. select userID, dt=rtrim((dt1+cast(left(dt2,2) as int)))+'小时'+substring(dt2,4,2)+'分'+substring(dt2,7,2)+'秒' from ( select userID, dt1=sum(datediff(dd,stopstarttime,stopendtime)*24), dt2=convert(varchar, dateadd(ms, sum(datediff(ms,'00:00:00.000',cast(convert(varchar,stopendtime-stopstarttime,114) as time))),'1900-01-01 00:00:00.000'), 114) from #t group by userID) t /* userID dt ----------- ---------------------------- 1 0小时31分49秒 2 720小时10分02秒 (2 行受影响) */
sql区惊现大Y [/quote] 2月最近忙啥呢 都没看出来说话
二月十六 版主 2019-07-05
  • 打赏
  • 举报
回复
引用 9 楼 吃货乙 的回复:
create table #t(userID int,stopstarttime datetime,stopendtime datetime,) insert into #t select 1,'2019-07-01 14:49:55.130','2019-07-01 14:50:37.903' union all select 1,'2019-07-01 14:50:42.717','2019-07-01 14:52:07.670' union all select 1,'2019-07-01 14:52:17.160','2019-07-01 14:52:19.880' union all select 1,'2019-07-01 14:52:23.523','2019-07-01 15:01:13.510' union all select 1,'2019-07-01 14:28:41.997','2019-07-01 14:28:48.410' union all select 1,'2019-07-01 14:29:00.307','2019-07-01 14:49:43.390' union all select 2,'2019-07-01 14:29:55.470','2019-07-01 14:31:14.657' union all select 2,'2019-07-01 14:31:59.673','2019-07-01 14:35:00.807' union all select 2,'2019-06-01 14:35:32.280','2019-07-01 14:41:14.110' -- 此处特意写06/01,以便测试小时计数. select userID, dt=rtrim((dt1+cast(left(dt2,2) as int)))+'小时'+substring(dt2,4,2)+'分'+substring(dt2,7,2)+'秒' from ( select userID, dt1=sum(datediff(dd,stopstarttime,stopendtime)*24), dt2=convert(varchar, dateadd(ms, sum(datediff(ms,'00:00:00.000',cast(convert(varchar,stopendtime-stopstarttime,114) as time))),'1900-01-01 00:00:00.000'), 114) from #t group by userID) t /* userID dt ----------- ---------------------------- 1 0小时31分49秒 2 720小时10分02秒 (2 行受影响) */
sql区惊现大Y
吃货乙 2019-07-05
  • 打赏
  • 举报
回复
create table #t(userID int,stopstarttime datetime,stopendtime datetime,) insert into #t select 1,'2019-07-01 14:49:55.130','2019-07-01 14:50:37.903' union all select 1,'2019-07-01 14:50:42.717','2019-07-01 14:52:07.670' union all select 1,'2019-07-01 14:52:17.160','2019-07-01 14:52:19.880' union all select 1,'2019-07-01 14:52:23.523','2019-07-01 15:01:13.510' union all select 1,'2019-07-01 14:28:41.997','2019-07-01 14:28:48.410' union all select 1,'2019-07-01 14:29:00.307','2019-07-01 14:49:43.390' union all select 2,'2019-07-01 14:29:55.470','2019-07-01 14:31:14.657' union all select 2,'2019-07-01 14:31:59.673','2019-07-01 14:35:00.807' union all select 2,'2019-06-01 14:35:32.280','2019-07-01 14:41:14.110' -- 此处特意写06/01,以便测试小时计数. select userID, dt=rtrim((dt1+cast(left(dt2,2) as int)))+'小时'+substring(dt2,4,2)+'分'+substring(dt2,7,2)+'秒' from ( select userID, dt1=sum(datediff(dd,stopstarttime,stopendtime)*24), dt2=convert(varchar, dateadd(ms, sum(datediff(ms,'00:00:00.000',cast(convert(varchar,stopendtime-stopstarttime,114) as time))),'1900-01-01 00:00:00.000'), 114) from #t group by userID) t /* userID dt ----------- ---------------------------- 1 0小时31分49秒 2 720小时10分02秒 (2 行受影响) */
唐诗三百首 2019-07-03
  • 打赏
  • 举报
回复

create table #t(userID int,stopstarttime datetime,stopendtime datetime,)

insert into #t
 select 1,'2019-07-01 14:49:55.130','2019-07-01 14:50:37.903' union all
 select 1,'2019-07-01 14:50:42.717','2019-07-01 14:52:07.670' union all
 select 1,'2019-07-01 14:52:17.160','2019-07-01 14:52:19.880' union all
 select 1,'2019-07-01 14:52:23.523','2019-07-01 15:01:13.510' union all
 select 1,'2019-07-01 14:28:41.997','2019-07-01 14:28:48.410' union all
 select 1,'2019-07-01 14:29:00.307','2019-07-01 14:49:43.390' union all
 select 2,'2019-07-01 14:29:55.470','2019-07-01 14:31:14.657' union all
 select 2,'2019-07-01 14:31:59.673','2019-07-01 14:35:00.807' union all
 select 2,'2019-06-01 14:35:32.280','2019-07-01 14:41:14.110'            -- 此处特意写06/01,以便测试小时计数.


select userID,
       dt=rtrim((dt1+cast(left(dt2,2) as int)))+'小时'+substring(dt2,4,2)+'分'+substring(dt2,7,2)+'秒'
 from (
select userID,
       dt1=sum(datediff(dd,stopstarttime,stopendtime)*24),
       dt2=convert(varchar,
                   dateadd(ms,
                           sum(datediff(ms,'00:00:00.000',cast(convert(varchar,stopendtime-stopstarttime,114) as time))),'1900-01-01 00:00:00.000'),
                   114)
 from #t
 group by userID) t

/*
userID      dt
----------- ----------------------------
1           0小时31分49秒
2           720小时10分02秒

(2 行受影响)
*/
RINK_1 2019-07-02
  • 打赏
  • 举报
回复
不知道你要的时分秒要怎么显示,试试下面的

SELECT USERID,CAST(SUM(DATEDIFF(HH,STOPSTARTTIME,STOPENDTIME)) AS VARCHAR)+'小时'
+CAST(SUM(DATEDIFF(MINUTE,STOPSTARTTIME,STOPENDTIME)) AS VARCHAR)+'分钟'
+CAST(SUM(DATEDIFF(SECOND,STOPSTARTTIME,STOPENDTIME)) AS VARCHAR)+'秒'
FROM TABLE  
GROUP BY USERID
RINK_1 2019-07-02
  • 打赏
  • 举报
回复
引用 4 楼 ballatong 的回复:
60秒的向分进位,60分的向小时进位,小时就保留小时 比如 100 小时23分40秒


SELECT USERID,CAST(TOTAL/3600 AS VARCHAR)+'小时'+CAST((TOTAL%3600)/60 AS VARCHAR)+'分钟'+CAST((TOTAL%3600)%60 AS VARCHAR)+'秒'
FROM
(SELECT USERID,SUM(DATEDIFF(SECOND,STOPSTARTTIME,STOPENDTIME)) AS TOTAL
FROM TABLE  
GROUP BY USERID) AS A

insus 2019-07-02
  • 打赏
  • 举报
回复
或者可以参考C#的方法:
https://www.cnblogs.com/insus/p/7997623.html
ballatong 2019-07-02
  • 打赏
  • 举报
回复
60秒的向分进位,60分的向小时进位,小时就保留小时 比如 100 小时23分40秒
RINK_1 2019-07-02
  • 打赏
  • 举报
回复
引用 2 楼 ballatong 的回复:
试过了结果不对
如果超过60秒、60分、24小时,要怎么显示呢
ballatong 2019-07-02
  • 打赏
  • 举报
回复
试过了结果不对
课程内容:本课程是《Java工程师必学系列课程》的第6部分,主要讲解Java语言中中新旧两代日期时间相关的类、日期时间相关的处理方法、历法和时间计算的常识,在课程的最后还安排了万年历实战项目本课程涉及的主要内容可以分为四部分:一、Java语言旧日期时间系统二、Java8新日期时间系统三、日期、时间、历法基础知识四、万年历项目课程说明:在开发Java程序的过程中,无论做什么类型的项目,基本上都会处理与日期和时间相关的问题。既然日期时间问题处理已经成了程序必须要掌握的技能,那我们就必须认真深入的学习日期时间的计算和处理方法。本课程将深入讲解Java语言新旧两代日期时间系统的相关知识。在讲解的过程中,不仅仅讲解如何调用各种类所提供的方法去处理相关问题,更是从原理上深入分析了这些类的设计原理,以及要如何避免在实战中踩到那些非常隐蔽的大坑。此外,除了讲解日期时间相关工具类的使用,本课程还向广大学员普及了一些必要的日期、时间和历法的相关知识。让学员能够从根本上理解日期时间算法的设计思路。同时,本课程在最后一部分,安排了非常精彩的、完整的万年历项目,通过实战的形式切实帮助学员提高解决具体问题的能力!预期效果:认真学习完本课程,学员可以掌握日期时间计算和处理的相关知识,并能提高实际的编码水平。配套福利:万年历软件的完整源码环境配置要求:学习本课程需安装JDK13或更高版本的JDK,以便程序能正确运行,建议使用IntelliJ IDEA 2019.1.2或更高版本的开发工具。因有合作协议约束,《穆哥学堂》只提供PDF版本的课件!

34,594

社区成员

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

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