Hi. I want to be able to calculate weekly averages of our report data. Here is a sample from our EmplyeeTrends table:
EmployeeID ReportDate TotalCases
2 1/1/2007 77
2 1/2/2007 63
3 1/1/2007 56All the report data is produced daily. However, my boss wants to be able to see weekly/monthly averages for each employee. Is this possible? Thanks so much for your help!select datepart(wk,ReportDate) as week
, avg(TotalCases)
from daTable
group
by datepart(wk,ReportDate)|||Yeah, but that will group weeks from different years together. I'd suggest grouping by year as well, or using this:
select dateadd(wk, datediff(wk, 0, ReportDate), 0) as week,
avg(TotalCases)
from daTable
group by dateadd(wk, datediff(wk, 0, ReportDate), 0)|||that's gorgeous, nice one blindman
:)|||thanks so much blindman and r937! those solutions work perfectly!
is it possible to find the average of the TotalCases by week as well so i can see the entire office's total cases? Does that make any sense? Thanks for your help!|||Yes, it is possible, but not advisable. You should not take averages of subtotals, as it skews your results. Weeks with fewer entries will be weighted more heavily than weeks with more entries.
It is best just to take an average across the entire office.|||i'll do that then. thanks so much for the advice!|||i'll do that then. thanks so much for the advice! wow, you don't see that very often around here...|||they're a heartless bunch, users, but the occasional good one like bla4free is why we do this|||Speak for yourself, Rudy...I do it simply for the chicks. Chicks dig database nerds.
Showing posts with label weekly. Show all posts
Showing posts with label weekly. 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)