Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 !!
Posted

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

SQL
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
 
Share this answer
 
v2
Comments
Member 8089110 28-Jun-13 5:52am    
Correct Answer & Thanks for your guidelines
ArunRajendra 28-Jun-13 6:15am    
welcome. Can you mark it is answered so that it will be useful for others.
Member 8089110 1-Jul-13 1:13am    
Hi friends,can you give me another way solutions because in the third columns gives only time first time '9:30 am' its in time and last time is 6:30pm out time they have not inserted any field 'in' & 'out' field in time columns.
Thanks in advanced !!
ArunRajendra 1-Jul-13 2:14am    
Try this code

Select sum(DATEDIFF(mi,intime.tim,outtime.tim)) From
(
select row_number() over( order by indata.tim asc) id,indata.tim,indata.dat from (select row_number() over( order by tim asc) id,tim,dat from @emptrack) indata where id%2='1') intime
Inner Join ( select row_number() over( order by outdata.tim asc) id,outdata.tim,outdata.dat from (select row_number() over( order by tim asc) id,tim,dat from @emptrack) outdata where id%2='0'
)outtime
on outtime.id =intime.id
group by intime.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[^]
 
Share this answer
 
Comments
Member 14738635 1-Jun-20 8:59am    
Or kesa ha bhai?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900