cpuphantom
IS-IT--Management
Hello,
I am attempting to program a pretty challenging Stored Procedure in SQL 7. I thought I'd put it up on here because you all might think of something I have not.
Here's what I'm trying to do. I am trying to do a real time desk count for users in an office for a reporting time period. A desk is a person working there. Normally this would be pretty easy to do, but we have a lot of turn over, so if a person worked for our office for 6 months... and I'm running a report for a year... that person would actually only be 0.5 of a desk. I actually need to be as specific is by month, so if a person worked for 1 month out of the year, they would be 1/12 of a desk.
I track in the tbUser table the hire_date and Term_date of each user. Using those dates, and the DateDiff function I believe I can get a single number returned for the total number of desks for a time period.
Here's an example of the table:
Now the stored procedure would require an office ID, a report start date, and a report end date.
So if we assume we want to use a reporting date of 1/1/2004 to 12/31/2004... I'd want to output a single number when that stored procedure is run that is equal to the desks of people working during the time period of 2004.
The number this table should produce is: 3.25
Why 3.25? Because if you use DateDiff to calculate that each person was there the following number of months:
The sum of those months is 39. Divid that by 12 (the # of months in the reporting period) that gives you 3.25 desks for the year.
This is a toughy... but figured someone out there would like the challenge. Let me know what you think would be a good stored procedure code to make this happen.
I am attempting to program a pretty challenging Stored Procedure in SQL 7. I thought I'd put it up on here because you all might think of something I have not.
Here's what I'm trying to do. I am trying to do a real time desk count for users in an office for a reporting time period. A desk is a person working there. Normally this would be pretty easy to do, but we have a lot of turn over, so if a person worked for our office for 6 months... and I'm running a report for a year... that person would actually only be 0.5 of a desk. I actually need to be as specific is by month, so if a person worked for 1 month out of the year, they would be 1/12 of a desk.
I track in the tbUser table the hire_date and Term_date of each user. Using those dates, and the DateDiff function I believe I can get a single number returned for the total number of desks for a time period.
Here's an example of the table:
Code:
Name hire_date term_date office_id
Reagan J 6/1/1998 null 1
Sam H 1/1/2001 null 1
Joe G 1/1/2002 5/31/2004 1
Mike C 7/1/2004 null 1
Leith L 1/1/2003 2/1/2004 1
Jay 2/1/2004 4/30/2004 1
Now the stored procedure would require an office ID, a report start date, and a report end date.
Code:
CREATE PROCEDURE spCalcDesks
@office_id int,
@startdate datetime,
@enddate datetime
AS
So if we assume we want to use a reporting date of 1/1/2004 to 12/31/2004... I'd want to output a single number when that stored procedure is run that is equal to the desks of people working during the time period of 2004.
The number this table should produce is: 3.25
Why 3.25? Because if you use DateDiff to calculate that each person was there the following number of months:
Code:
Reagan 12
Sam 12
Joe 5
Mike 6
Leith 1
Jay 3
The sum of those months is 39. Divid that by 12 (the # of months in the reporting period) that gives you 3.25 desks for the year.
This is a toughy... but figured someone out there would like the challenge. Let me know what you think would be a good stored procedure code to make this happen.