在Sql Server 2008中计算时间 [英] Calculation of Time in Sql Server 2008

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

问题描述

亲爱的朋友,

我在实现查询时遇到了一些问题。我构建了一个存在三列的表,第一个是employeeid,第二个是日期和最后一次。问题如下: -



考虑公司工作时间从9:30到6:30开始。当我进入办公室,因为我的员工是1,今天的日期是2013年6月28日,我的办公时间是上午9:30,然后我有时间在办公室工作,然后在上午11:00出来快速休息,上午11点15分来到办公室。后来我在办公室下午1点吃午饭,下班时间下午1:30上班。这可以按照以下方式办理: -





EmpId日期时间

1 28-jun-2013 9:30 am ----> in

11:00 am -----> out

上午11:15 ---->在

01:00 pm -----> out

01:30 pm ----->在

03:00 pm ------> out

03:10 pm ----- >在

05:00 pm ------> out

05:15 pm ------>在

06:30 pm ------> out





问题如下: -





我想计算一下办公室有多少工作时间?有多少时间不在办公室工作?



这只能通过单一查询完成,而不是由任何列实现。



谢谢你提前!!

解决方案

可能不会有所帮助,但至少应该给出一些想法。希望这会有所帮助。



 声明  @ emptrack   table  

dat date
tim 时间
inout varchar 3


插入 进入 @ emptrack (GETDATE(),' 上午9:30'' in'
insert into @ emptrack (GETDATE(),' 上午11:00'' out'
插入 进入 @ emptrack (GETDATE(),' 11:15 am'' in'
insert 进入 @ emptrack ( GETDATE(),' 1:00 pm'' out'
插入 进入 @ emptrack (GETDATE(),' 1:30 pm'' in'
插入 进入 @ emptrack (GETDATE(),' 3:00 pm'' out'
insert 进入 @ emptrack (GETDATE (),' 3:10 pm'' in'
插入 进入 @ emptrack (GETDATE(),' '下午5:00'' out'
插入 进入 @ emptrack (GETDATE(),' 5:15 pm'' in'
insert into @ emptrack values (GETDATE(),< span class =code-string>' 6:30 pm'' < span class =code-string> out')

选择 sum(DATEDIFF(mi,a.tim,b.tim))
选择 row_number() over order by tim asc )id,tim,dat 来自 @ emptrack 其中 inout = ' 在')a
完整 加入选择 row_number() over order by tim asc )id,tim,dat 来自 @ emptrack 其中 inout = ' out' )b
on a.id = b.id
group by a.dat


如果你计算时间,你必须要添加列,因为你如何及时识别它或者OUT时间。

然后你可以计算出时间和时间的总小时数。

但是在我看来你必须添加两个这样的列

EmpID日期InTime OutTime状态(输入/输出)。





可以帮助您:

http://stackoverflow.com/questions/ 3836439 / find-total-hours-hours-between-two-dates [ ^

Dear Friend,
I have some problem when implementing a query.I construct a table that exists three columns first is employeeid,second is date & last one time.The problem is as follows :-

consider The company working office time started 9:30 to 6:30. When i am entered in the office as my employeeid is 1,today date is 28-jun-2013 and my in time office is 9:30am then i am some time work in the office then out at time 11:00am for break fast and come in the office 11:15am.Later i am out in the office 1:00pm for lunch and come in office 1:30pm for work.This can be done as follows:-


EmpId Date Time
1 28-jun-2013 9:30am---->in
11:00am----->out
11:15am---->in
01:00pm----->out
01:30pm----->in
03:00pm------>out
03:10pm----->in
05:00pm------>out
05:15pm------>in
06:30pm------>out


The problem is as follows :-


I want to calculated "How many working time in the office ? and how many time not work in the office ?"

This can be done only by single query and not implemented by any columns.

Thanks in advanced !!

解决方案

May not be accurete but atleast should give some idea. Hope this will help.

declare @emptrack table
(
dat date,
tim time,
inout varchar(3)
)

insert into @emptrack values(GETDATE(),'9:30 am','in')
insert into @emptrack values(GETDATE(),'11:00 am','out')
insert into @emptrack values(GETDATE(),'11:15 am','in')
insert into @emptrack values(GETDATE(),'1:00 pm','out')
insert into @emptrack values(GETDATE(),'1:30 pm','in')
insert into @emptrack values(GETDATE(),'3:00 pm','out')
insert into @emptrack values(GETDATE(),'3:10 pm','in')
insert into @emptrack values(GETDATE(),'5:00 pm','out')
insert into @emptrack values(GETDATE(),'5:15 pm','in')
insert into @emptrack values(GETDATE(),'6:30 pm','out')

Select sum(DATEDIFF(mi,a.tim,b.tim)) From 
(select row_number() over( order by tim asc) id ,  tim,dat from @emptrack where inout='in')a
Full Outer Join (select row_number() over( order by tim asc) id ,  tim,dat from @emptrack where inout='out')b 
on a.id=b.id 
group by a.dat


If you calculate time you must have to add column because how you identify it IN time or OUT time.
and then you can calculate total hour of in time and out time.
But at my point of view you have to add two column like this
EmpID Date InTime OutTime Status(In/Out).


May be this on help you:
http://stackoverflow.com/questions/3836439/find-total-number-of-hours-between-two-dates[^]


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

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