在数据库中插入每天完成的工作时间,并获取以HH:MM格式完成任务所花费的总时间 [英] Insert time of work done per day in Database and fetch total time taken to complete the task in HH:MM format

查看:73
本文介绍了在数据库中插入每天完成的工作时间,并获取以HH:MM格式完成任务所花费的总时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从下拉列表中获取一天的开始时间和结束时间

每天条目将以HH:MM格式转到数据库

如1小时15分钟或1.15



1周后,我们需要以HH:MM格式在1周内完成工作总时间。



总时间:HH:MM

解决方案

您好Ashok,我认为您可以通过在表中使用两个字段作为StartTime来实现此目的。 EndTime,数据类型为time(7)。然后你可以保存你的数据这些字段。



要获取你可能做的总时间,如下所示



  SELECT   CONVERT  NUMERIC  18  2 ),SUM(DATEDIFF(分钟,StartTime,EndTime)) /  60  +(SUM(DATEDIFF(分钟,开始时间,结束时间))% 60 )/  100  0 
FROM TaskEntry < span class =code-comment> - 其中TaskEntry是您的名字


< blockquote>如果将数据保存为日期时间,则可以使用DateDiff(mi,开始日期,结束日期)来获取开始时间和结束时间之间的差异(以分钟为单位)。然后在整个日期范围(一周)内总计得到总分钟数。



最后,除以60得到小时数。这应该是整数。这是数字的模数,60是剩余的分钟数。

  SELECT  total_time = SUM(minutes_worked ),hours = FLOOR(SUM(minutes_worked)/  60 ),
分钟= SUM(minutes_worked)% 60
FROM

SELECT work_date,minutes_worked = DATEDIFF(' mi',start,stop) FROM your_table
WHERE work_date BETWEEN first_day_of_the_week AND end_date_of_the_week





如果这有帮助,请花些时间接受解决方案。谢谢。


I need to get the start time and end time for a day from dropdownlists
per day entries will go to database in HH:MM format
like 1 hr and 15 min OR 1.15

After 1 week we need to get the total time of work done in 1 week in HH:MM format.

Total Time : HH:MM

解决方案

Hi Ashok, I think you can achieve this by having 2 fields in your table as StartTime and EndTime with datatype as time(7). Then you can save your data these fields.

To fetch the total time you might do as below

SELECT CONVERT(NUMERIC(18, 2), SUM(DATEDIFF(minute,StartTime,EndTime)) / 60 + (SUM(DATEDIFF(minute,StartTime,EndTime)) % 60) / 100.0)
FROM TaskEntry --Where TaskEntry is yourtable name


If you save your data as datetime, you can use DateDiff(mi, start date, end date) to get the difference between start time and end time in minutes per day. Then sum that across the date range (one week) to get total number of minutes.

Finally, divide it by 60 to get number of hours. This should be integer. THen do modulo of the number and 60 to get remainder of minutes.

SELECT total_time = SUM(minutes_worked), hours = FLOOR(SUM(minutes_worked) / 60),
minutes = SUM(minutes_worked) % 60
FROM
(
SELECT work_date, minutes_worked = DATEDIFF('mi', start, stop) FROM your_table
WHERE work_date BETWEEN first_day_of_the_week AND end_date_of_the_week
)



If this helps, please take time to accept the solution. Thank you.


这篇关于在数据库中插入每天完成的工作时间,并获取以HH:MM格式完成任务所花费的总时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆