473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

how to fetch total working hours from the table for every employee for a week

9
hi,
I am new to sq server, and
I want to calculate the total working hours for all the employees excluding their break. This is the sample table for 2 employes.

logID logDate logTime srno

2 12/19/2011 0:00 1899-12-30 16:36:16.000 933252
2 12/19/2011 0:00 1899-12-30 11:21:20.000 933137
2 12/20/2011 0:00 1899-12-30 15:55:33.000 933636
2 12/20/2011 0:00 1899-12-30 15:55:18.000 933635
2 12/20/2011 0:00 1899-12-30 12:18:25.000 933539
2 12/22/2011 0:00 1899-12-30 16:07:25.000 934472
2 12/22/2011 0:00 1899-12-30 10:47:49.000 934361
2 12/23/2011 0:00 1899-12-30 16:00:55.000 934841
2 12/23/2011 0:00 1899-12-30 11:02:04.000 934734
6 12/19/2011 0:00 1899-12-30 21:15:33.000 933350
6 12/19/2011 0:00 1899-12-30 19:09:05.000 933308
6 12/19/2011 0:00 1899-12-30 18:45:04.000 933298
6 12/19/2011 0:00 1899-12-30 18:43:33.000 933297
6 12/19/2011 0:00 1899-12-30 16:51:43.000 933261
6 12/19/2011 0:00 1899-12-30 16:15:37.000 933244
6 12/19/2011 0:00 1899-12-30 13:57:20.000 933205
6 12/19/2011 0:00 1899-12-30 13:42:36.000 933196
6 12/19/2011 0:00 1899-12-30 12:17:06.000 933149
6 12/20/2011 0:00 1899-12-30 18:55:45.000 933704
6 12/20/2011 0:00 1899-12-30 16:36:05.000 933648
6 12/20/2011 0:00 1899-12-30 14:09:59.000 933601
6 12/20/2011 0:00 1899-12-30 13:41:49.000 933588
6 12/20/2011 0:00 1899-12-30 12:33:03.000 933543
6 12/21/2011 0:00 1899-12-30 18:40:58.000 934110


Thanks in advance
Jan 31 '12 #1
9 3049
Rabbit
12,516 Expert Mod 8TB
Why is date separated from time? They should be together. I have no idea how the records are related to each other, so there's no where enough information to suggest a solution.
Jan 31 '12 #2
ssoni
9
hi,
I know, but only this database is available to me. I have to work on it. Can you suggest any cursor program or something.

Thanks.
Feb 1 '12 #3
Rabbit
12,516 Expert Mod 8TB
I can not because you never told us how the data is related and what the end results are that you're looking for.
Feb 1 '12 #4
ssoni
9
That is one table stored in SQL SERVER and I have to use that to find the total working hour for each employee for one week.

I have written a SP for that but it was showing inconsistent result.
I have taken date part from first datetime column and timepart from other datetime column.

The SP is as follows-
create procedure sp_workhours
as
begin
declare @logID int
declare @logDate datetime
declare @logTime datetime
declare @srno int
declare @count int
declare @checkID int
declare @workhrs int =0
declare @fromDate datetime
declare @toDate datetime
set @count=0

/*declare cursor to calculate total working hours*/
declare workhours cursor for
select * from logtable where logDate between '2011-12-19' and '2011-12-23' order by logID, logDate, logTime;
open workhours;

fetch workhours into
@logID,@logDate,@logTime,@srno

WHILE @@FETCH_STATUS = 0
begin
set @count=@count+1

if(@checkID = @logID or @count = 1)
begin
if((@count%2=1 or @count = 1) )
begin

set @checkID = @logID
set @fromDate = convert(datetime,(CONVERT(varchar(11), @logDate, 112)+' ' +CONVERT(VARCHAR(20), @logTime, 114)))
print 'from'
print @fromDate

end
else if(@count%2=0)
begin
set @toDate = convert(datetime,(CONVERT(varchar(11), @logDate, 112)+' ' +CONVERT(VARCHAR(20), @logTime, 114)))
print @toDate


if( DATEDIFF(day ,@fromDate, @toDate)=0 )
begin

set @workhrs = @workhrs + DATEDIFF(SECOND ,@fromDate, @toDate)
end

else
begin
print 'notmath'
print 'todate'
print @toDate
set @fromDate =null
set @toDate = null
end

print @workhrs/3600
end
end
else
begin

declare @ResTable Table
(
logID int,
workhrs float
)
insert into @ResTable (logID,workhrs) values(@checkID ,@workhrs/3600 )


-- select logID, from logtable order by logDate,logID
set @count =0
set @workhrs=0
end

fetch next from workhours into
@logID,@logDate,@logTime,@srno
end
close workhours
select * from @ResTable

end
Feb 1 '12 #5
Rabbit
12,516 Expert Mod 8TB
You've already said you're trying to get working hours. That still tells me nothing about how the records are related to each other nor does it tell me what the end results should look like.
Feb 1 '12 #6
ssoni
9
I'm trying to get the no. of hours each employee worked for the complete week. In the end result i want employee and his total working hour. The Date and Time values are related to each other.

thanks
Feb 2 '12 #7
Rabbit
12,516 Expert Mod 8TB
You've already said all that. I need you to tell me how you calculate the working hours.
Feb 2 '12 #8
ssoni
9
ok. i try to take the date value from the first date time col. and time value from other col. and take the diff of every two even and odd time value.
here, now want that if the time value has not other matching even value of time it must be skipped on the same date. and it again start new calculation for new date. that is how it calculate the working hour for the week.

i m not sure whether my logic is correct.
but that is what i want to do.

thanks.
Feb 2 '12 #9
Rabbit
12,516 Expert Mod 8TB
What does that even mean? What is an "even" time value? What is an "odd" time value? What is a "matching even value of time"?

What does the data in the table even represent?
Feb 2 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
14
by: niknokio | last post by:
Hi I need to be able to calculate the working time between a StartDate and an EndDate. the data is in dd/mm/yyyy hh:mm:ss format and given an 8.5 hr day (weekdays). Ive found lots of really...
1
by: deena22 | last post by:
hello, i'm using 'Access database' and VB 6.0. My database is named ' timesheet' and it contains a table named 'tabletimesheet'. The table contain the following fields: 'staffname, stafftype,...
1
by: Raya | last post by:
Hi, I have a requirement as follows: I have a COBOL+DB2 program that inserts data into a DB2 table when an order is placed. Now, The same data should be inserted into the same table after every...
1
by: pavlibeis | last post by:
Hello. I have this problem. I have i site with classifieds based on mysql and i want to put products from e-shops. One e-shop wants me to update my data base every 6 hours from an xml file or csv...
3
by: abs0lut01 | last post by:
I know how to select the members who did not post for every topic. I need to send an email every 24 hours to the members who did not post for every topic. Could you please help me?
10
bsmeena7005
by: bsmeena7005 | last post by:
Hi help me to calculate the working hours between 10 to 18 hrs between two dates including the weekends also no braek or lunch time Example :- start date #01-08-2009 08:45:23# end date ...
1
Sl1ver
by: Sl1ver | last post by:
Hi my problem currently is that i need to get database on working hours(7am-5pm) so lets say a order lies in the database for e.g 4 days if i just get like timediff i will end up 96 hours...
0
kirubagari
by: kirubagari | last post by:
I'm having the issue where unable fetch the data if the value in combo box is chosen. The problem is when user choose the value from combo box,i want the code fetch for the rest data from the table....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.