Hello,
I'm calculating with 'datetime' fields and have the following
question/problem.
To get a period I subtract the field <Timefrom> from the field
<Timetill>. Both field have the type 'datetime'.
2005-09-22 15:00:00.000 - 2005-09-22 13:30:00.000
= 1900-01-01 01:30:00.000
This is working OK.
After this I would like to sum the results.
Then I get the message:
[Server: Msg 409, Level 16, State 2, Line 1
The sum or average aggregate operation cannot take a datetime data type
as an argument.]
How can I solve this, keeping the time (hours and minutes)?
NB. In my situation the period is smaller than 1 day.
Hans
*** Sent via Developersdex http://www.examnotes.net ***Take a look a DATEDIFF system function.
"Hans" <nospam@.devdex.com> wrote in message
news:%23Ma0eF2vFHA.2728@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I'm calculating with 'datetime' fields and have the following
> question/problem.
> To get a period I subtract the field <Timefrom> from the field
> <Timetill>. Both field have the type 'datetime'.
> 2005-09-22 15:00:00.000 - 2005-09-22 13:30:00.000
> = 1900-01-01 01:30:00.000
> This is working OK.
> After this I would like to sum the results.
> Then I get the message:
> [Server: Msg 409, Level 16, State 2, Line 1
> The sum or average aggregate operation cannot take a datetime data type
> as an argument.]
> How can I solve this, keeping the time (hours and minutes)?
> NB. In my situation the period is smaller than 1 day.
> Hans
> *** Sent via Developersdex http://www.examnotes.net ***|||SELECT
DATEADD(MI,SUM(DATEDIFF(MI,timefrom,time
till)),0)
FROM YourTable ;
or
SELECT CONVERT(CHAR(5),
DATEADD(MI,SUM(DATEDIFF(MI,timefrom,time
till)),0),14)
FROM YourTable ;
David Portas
SQL Server MVP
--|||Hwo about some custom formatting for that, and the use of datediff:
CREATE FUNCTION Formatminutes
(
@.Minutes INT
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @.Hours INT
SET @.Hours = @.minutes/60
SEt @.Minutes = @.Minutes - @.hours * 60
RETURN (Select CAST(@.hours AS VARCHAR(10)) + ':' + RIGHT('00' +
CAST(@.Minutes AS VARCHAR(10)),2))
END
Select dbo.Formatminutes(ABS(Datediff(mi,'2005-09-22
15:01:00.000','2005-09-22 13:30:00.000')))
HTH, jens Suessmeyer.|||try this
SELECT FLOOR(SUM(DATEDIFF(SS,startdate,enddate)
)/60) as Minitues,
SUM(DATEDIFF(SS,startdate,enddate))%60 as seconds FROM <tablename>
use can straigh away use 'm' in place of ss also in the first one
Regards
R.D
"Uri Dimant" wrote:
> Take a look a DATEDIFF system function.
>
> "Hans" <nospam@.devdex.com> wrote in message
> news:%23Ma0eF2vFHA.2728@.TK2MSFTNGP14.phx.gbl...
>
>
Showing posts with label period. Show all posts
Showing posts with label period. Show all posts
Thursday, February 16, 2012
Sunday, February 12, 2012
Calculating employees weekly hours
The pay period begins on a sunday and ends on a saturday. Some
employees work on the w
ends, some don't.
How can I calculate by w
how many hours an employee has worked?
Date EmpNumber Hours
20050522(sun) 123 7.5
20050523(mon) 123 8
20050524(tues) 123 8.5
20050523(mon) 246 9
20050524(tues) 246 6.5
20050526(thur) 246 7.5
20050529(sun) 345 8
20050530(mon) 345 7
20050531(tues) 345 6.75
I need the outcome to look like this:
PayPeriodW
EmpNumber Hours
20050522 123 24
20050522 246 23
20050529 345 21.75Hi ninel,
try this:
****************************************
**********
create table test_data(
MyDate datetime,
EmpNumber int,
Hours decimal(9,2)
)
go
insert into test_data (MyDate, EmpNumber, Hours)
values ('22 May 2005', 123, 7.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('23 May 2005', 123, 8)
insert into test_data (MyDate, EmpNumber, Hours)
values ('24 May 2005', 123, 8.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('23 May 2005', 246, 9)
insert into test_data (MyDate, EmpNumber, Hours)
values ('24 May 2005', 246, 6.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('26 May 2005', 246, 7.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('29 May 2005', 345, 8)
insert into test_data (MyDate, EmpNumber, Hours)
values ('30 May 2005', 345, 7)
insert into test_data (MyDate, EmpNumber, Hours)
values ('31 May 2005', 345, 6.75)
go
select * from test_data
go
select min(MyDate) as min_MyDate, EmpNumber, datepart(ww, MyDate), sum(Hours
)
from test_data
group by EmpNumber, datepart(ww, MyDate)
****************************************
**********
SQL Server has Date Function to get work w
of a year.
please refer BOL to check out DatePart function.
However, if your first day of w
is different, you may need to use other
way to calculate.
Leo Leong
"ninel" wrote:
> The pay period begins on a sunday and ends on a saturday. Some
> employees work on the w
ends, some don't.
> How can I calculate by w
how many hours an employee has worked?
> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours
> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
>
employees work on the w
ends, some don't.How can I calculate by w
how many hours an employee has worked?Date EmpNumber Hours
20050522(sun) 123 7.5
20050523(mon) 123 8
20050524(tues) 123 8.5
20050523(mon) 246 9
20050524(tues) 246 6.5
20050526(thur) 246 7.5
20050529(sun) 345 8
20050530(mon) 345 7
20050531(tues) 345 6.75
I need the outcome to look like this:
PayPeriodW
EmpNumber Hours20050522 123 24
20050522 246 23
20050529 345 21.75Hi ninel,
try this:
****************************************
**********
create table test_data(
MyDate datetime,
EmpNumber int,
Hours decimal(9,2)
)
go
insert into test_data (MyDate, EmpNumber, Hours)
values ('22 May 2005', 123, 7.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('23 May 2005', 123, 8)
insert into test_data (MyDate, EmpNumber, Hours)
values ('24 May 2005', 123, 8.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('23 May 2005', 246, 9)
insert into test_data (MyDate, EmpNumber, Hours)
values ('24 May 2005', 246, 6.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('26 May 2005', 246, 7.5)
insert into test_data (MyDate, EmpNumber, Hours)
values ('29 May 2005', 345, 8)
insert into test_data (MyDate, EmpNumber, Hours)
values ('30 May 2005', 345, 7)
insert into test_data (MyDate, EmpNumber, Hours)
values ('31 May 2005', 345, 6.75)
go
select * from test_data
go
select min(MyDate) as min_MyDate, EmpNumber, datepart(ww, MyDate), sum(Hours
)
from test_data
group by EmpNumber, datepart(ww, MyDate)
****************************************
**********
SQL Server has Date Function to get work w
of a year.please refer BOL to check out DatePart function.
However, if your first day of w
is different, you may need to use otherway to calculate.
Leo Leong
"ninel" wrote:
> The pay period begins on a sunday and ends on a saturday. Some
> employees work on the w
ends, some don't.> How can I calculate by w
how many hours an employee has worked?> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
>
Calculating employees weekly hours
The pay period begins on a sunday and ends on a saturday. Some employees
work on the w
ends, some don't.
How can I calculate by w
how many hours an employee has worked?
Date EmpNumber Hours
20050522(sun) 123 7.5
20050523(mon) 123 8
20050524(tues) 123 8.5
20050523(mon) 246 9
20050524(tues) 246 6.5
20050526(thur) 246 7.5
20050529(sun) 345 8
20050530(mon) 345 7
20050531(tues) 345 6.75
I need the outcome to look like this:
PayPeriodW
EmpNumber Hours
20050522 123 24
20050522 246 23
20050529 345 21.75
Message posted via http://www.webservertalk.comBest way ould be to use a calendar table to join to and evaluate the
results:
Then you could be with:
Select ct.w
no,sum(Hours)
From TimeTable tt
inner join Calendartable ct
on ct.date = tt.date
The first entry of the w
can be easily extracted from the calendar table,
depending if you want to show the day (sunday) wheter a employee has worked
there ( --> inner join) or even if no employee worked there ((perhpas if
nobody worked on sunday the monday will be displayed -- left join / outer
join)
See the functions for calendar tale here at :
http://www.aspfaq.com/show.asp?id=2519
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"ninel gorbunov via webservertalk.com" <forum@.nospam.webservertalk.com> schrieb im
Newsbeitrag news:6d37fd5c688b46efbabd32da05b5f85f@.SQ
webservertalk.com...
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.
> How can I calculate by w
how many hours an employee has worked?
> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours
> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com|||First, check your server to see if the first day of the w
is set as
Sunday. You can do this using @.@.DATEFIRST. If you get 1 for SELECT
DATEPART( dw, '20050522' ), you can do:
SELECT dt - DATEPART( dw, dt ) + 1, EmpNumber, SUM( Hours )
FROM tbl
GROUP BY EmpNumber, dt - DATEPART( dw, dt ) + 1 ;
Anith|||Assuming Sunday is set as your first day of the w
, you can try this:
SELECT
DATEADD(d, -DATEPART(dw, MIN(CAST(WorkDay AS DATETIME))) + 1,
MIN(CAST(WorkDay AS DATETIME))) PayPeriodW
,
EmpNumber,
SUM(Hours)
FROM Test1
GROUP BY
DATEPART(ww, CAST(WorkDay AS DATETIME)),
EmpNumber
Lionel Chacon
"ninel gorbunov via webservertalk.com" wrote:
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.
> How can I calculate by w
how many hours an employee has worked?
> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours
> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com
>|||Here's a query that will aggregate by the w
, with Sunday being considered
thee first day of the w
, independent of the DATEFIRST setting:
select empnumber, datecol - datepart(dw, datecol - @.@.datefirst - 7) + 1 as
w
start, sum(hours) as cnt
from yourtable
group by empnumber, datecol - datepart(dw, datecol - @.@.datefirst - 7) + 1
The trick with @.@.datefirst neutralizes the effect of the DATEFIRST setting.
I subtracted the constant 7 to consider Sunday as the first w
day. Since
w
days follow a cyclic axis from 1 through 7, I could have just omitted
the - 7 part (equal to -0).
But it's important to figure out that you subtract whichever number stands
for the logical DATEFIRST YOU want to set. If you want Monday to be
considered the first w
day, simply subtract 1 instead of 7. You can find
in BOL under DATEFIRST which number represents which first w
day.
BG, SQL Server MVP
www.SolidQualityLearning.com
"ninel gorbunov via webservertalk.com" <forum@.nospam.webservertalk.com> wrote in
message news:6d37fd5c688b46efbabd32da05b5f85f@.SQ
webservertalk.com...
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.
> How can I calculate by w
how many hours an employee has worked?
> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours
> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com
work on the w
ends, some don't.How can I calculate by w
how many hours an employee has worked?Date EmpNumber Hours
20050522(sun) 123 7.5
20050523(mon) 123 8
20050524(tues) 123 8.5
20050523(mon) 246 9
20050524(tues) 246 6.5
20050526(thur) 246 7.5
20050529(sun) 345 8
20050530(mon) 345 7
20050531(tues) 345 6.75
I need the outcome to look like this:
PayPeriodW
EmpNumber Hours20050522 123 24
20050522 246 23
20050529 345 21.75
Message posted via http://www.webservertalk.comBest way ould be to use a calendar table to join to and evaluate the
results:
Then you could be with:
Select ct.w
no,sum(Hours)From TimeTable tt
inner join Calendartable ct
on ct.date = tt.date
The first entry of the w
can be easily extracted from the calendar table,depending if you want to show the day (sunday) wheter a employee has worked
there ( --> inner join) or even if no employee worked there ((perhpas if
nobody worked on sunday the monday will be displayed -- left join / outer
join)
See the functions for calendar tale here at :
http://www.aspfaq.com/show.asp?id=2519
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"ninel gorbunov via webservertalk.com" <forum@.nospam.webservertalk.com> schrieb im
Newsbeitrag news:6d37fd5c688b46efbabd32da05b5f85f@.SQ
webservertalk.com...
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.> How can I calculate by w
how many hours an employee has worked?> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com|||First, check your server to see if the first day of the w
is set asSunday. You can do this using @.@.DATEFIRST. If you get 1 for SELECT
DATEPART( dw, '20050522' ), you can do:
SELECT dt - DATEPART( dw, dt ) + 1, EmpNumber, SUM( Hours )
FROM tbl
GROUP BY EmpNumber, dt - DATEPART( dw, dt ) + 1 ;
Anith|||Assuming Sunday is set as your first day of the w
, you can try this:SELECT
DATEADD(d, -DATEPART(dw, MIN(CAST(WorkDay AS DATETIME))) + 1,
MIN(CAST(WorkDay AS DATETIME))) PayPeriodW
,EmpNumber,
SUM(Hours)
FROM Test1
GROUP BY
DATEPART(ww, CAST(WorkDay AS DATETIME)),
EmpNumber
Lionel Chacon
"ninel gorbunov via webservertalk.com" wrote:
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.> How can I calculate by w
how many hours an employee has worked?> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com
>|||Here's a query that will aggregate by the w
, with Sunday being consideredthee first day of the w
, independent of the DATEFIRST setting:select empnumber, datecol - datepart(dw, datecol - @.@.datefirst - 7) + 1 as
w
start, sum(hours) as cntfrom yourtable
group by empnumber, datecol - datepart(dw, datecol - @.@.datefirst - 7) + 1
The trick with @.@.datefirst neutralizes the effect of the DATEFIRST setting.
I subtracted the constant 7 to consider Sunday as the first w
day. Sincew
days follow a cyclic axis from 1 through 7, I could have just omittedthe - 7 part (equal to -0).
But it's important to figure out that you subtract whichever number stands
for the logical DATEFIRST YOU want to set. If you want Monday to be
considered the first w
day, simply subtract 1 instead of 7. You can findin BOL under DATEFIRST which number represents which first w
day.BG, SQL Server MVP
www.SolidQualityLearning.com
"ninel gorbunov via webservertalk.com" <forum@.nospam.webservertalk.com> wrote in
message news:6d37fd5c688b46efbabd32da05b5f85f@.SQ
webservertalk.com...
> The pay period begins on a sunday and ends on a saturday. Some employees
> work on the w
ends, some don't.> How can I calculate by w
how many hours an employee has worked?> Date EmpNumber Hours
> 20050522(sun) 123 7.5
> 20050523(mon) 123 8
> 20050524(tues) 123 8.5
> 20050523(mon) 246 9
> 20050524(tues) 246 6.5
> 20050526(thur) 246 7.5
> 20050529(sun) 345 8
> 20050530(mon) 345 7
> 20050531(tues) 345 6.75
> I need the outcome to look like this:
> PayPeriodW
EmpNumber Hours> 20050522 123 24
> 20050522 246 23
> 20050529 345 21.75
> --
> Message posted via http://www.webservertalk.com
Subscribe to:
Posts (Atom)