如何计算Sql Server 2008中的总时间(查询) [英] How To Count Total Time In Sql Server 2008 (Query)

查看:166
本文介绍了如何计算Sql Server 2008中的总时间(查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL查询问题



我的列TotalTime数据类型nvarchar(50)= null



总时间= '15:00'每天

总时间= '15:00'每一天

总时间= '15:00'每天

总时间= '15:00'每一天



算最终总数= '12:00'(显示此结果以供我查询)



但实际结果是'60:00'



查询: -

< pre lang =sql> SELECT 转换 nvarchar 10 ),Datepart(HOUR,DATEADD(MINUTE,SUM(DATEDIFF(MINUTE,00:00;,TotalHour)),00:00))) +
转换 nvarchar 10 ),Datepart(MINUTE,DATEADD(MINUTE,SUM(DATEDIFF(MINUTE,00:00,TotalHour)),00:00))) as TotalHour
FROM tbl_EmployeeAttendance
其中 empId = ' 5' work_type = 1
DateName(月,DateAdd(月,DatePart(月,转换 datetime ,Attendance_Date,< span class =code-digit> 103 )), 0 ) - 1 ) = ' 11月' 年份(< span class =code-keyword> convert ( datetime ,Attendance_Date, 103 )) = ' 2014'





问题: - 如果总时间大于24小时,那么总时间计算开始00:00

但是我算上一小时的总时间...



forexp: -

总时间第1天12:00

总时间第1天13:00

总时间第1天11:00

总时间第1天11:00

总时间1第12天



最后总时间= 59:00





请帮助解决此问题

解决方案

DateTime,Date和Time数据类型用于存储点 - 时间而不是花费的时间...

正如你自己告诉你的那样,不能超过24小时记录,因为一天不超过24小时!!!

从这个意义上讲,你原来的设计是数据库错了!如果你想记录花费的时间(小时或分钟)使用一些整数(十进制)数据类型,那么如果你有一个花了36个小时的事件,你可以在36列(或2160分钟)写36简单的SUM就可以了!


My Problem in sql query

my column TotalTime datatype nvarchar(50)=null

Total Time = '15:00' Per Day
Total Time = '15:00'Per Day
Total Time = '15:00' Per Day
Total Time = '15:00'Per Day

count final total = '12:00' (Show This Result For my Query )

but Actual result is '60:00'

Query :-

SELECT Convert(nvarchar(10), Datepart(HOUR, DATEADD(MINUTE, SUM(DATEDIFF(MINUTE, 00:00;, TotalHour)), 00:00)))+
Convert(nvarchar(10), Datepart(MINUTE, DATEADD(MINUTE, SUM(DATEDIFF(MINUTE,00:00, TotalHour)), 00:00))) as TotalHour
FROM tbl_EmployeeAttendance
where empId='5' and work_type=1
and DateName( month , DateAdd( month , DatePart(Month,convert(datetime,Attendance_Date,103)) , 0 ) - 1 ) ='November' and YEAR(convert(datetime,Attendance_Date,103))= '2014'



Problem :- IF Total time is greater than 24 hour then totaltime calculate start 00:00
but i m count the total time in hour...

forexp:-
total time 1st day 12:00
total time 1st day 13:00
total time 1st day 11:00
total time 1st day 11:00
total time 1st day 12:00

Final Total Time = 59:00


Please Help this problem

解决方案

DateTime, Date and Time data types are used to store a point-int-time and not amount of time spent...
As you yourself told you can not record with it more than 24 hours as there is no more than 24 hours in a day!!!
In that sense you original design of the database is wrong! If you want to record the amount of time (hours or minutes) spent use some integer (decimal) data type, so if you have an event that took 36 hours you can write 36 in that column 36 (or 2160 for minutes) and do simple SUM over it!


这篇关于如何计算Sql Server 2008中的总时间(查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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