472,980 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SQL statement to compute employee pay for a year at different pay rates

I would like a single SQL to return all employee's total billable
compensation for a year. Their billable rates change throughout the year so
under the employee table (one), there is a compensation table (to many)
which has the employee id, effective date, billable hourly rate. So in a
given year calendar year they could have many different (though usually 2 at
most) rates. These rates then have to correspond to and e multiplied by
their corresponding billable hours from the time sheet table.

I know I could create a series of UNIONs and hard code the effective dates,
i.e.

select from time sheets where employee=john and timesheet.task_date between
jan 1 and jun 1, compensation.billable rate * timesheet.billable hours

UNION

select from time sheets where employee=john timesheet.task_date between jun
1 and dec 31 compensation.billable_rate * timesheet.billable_hours

I'd have to do that for every employee in a very large SQL.

Is there an easier way using straight SQL? If not could it be done with a
stored procedure?

Thanks for any insight.
Nov 23 '05 #1
2 1998
To do this it's necessary to know what your tables look like. In a
normalised db of this type you'd probably have 3 tables involved

EMPLOYEES
PAY RATES
TIMESHEET

with proper primary and foreign key relationships. If you have this kind
of arrangement then it's as easy as falling off a log. If not then it
depends what data you actually do have.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Nov 23 '05 #2
Chad Richardson (chad@NIXSPAM_chadrichardson.com) writes:
I would like a single SQL to return all employee's total billable
compensation for a year. Their billable rates change throughout the year
so under the employee table (one), there is a compensation table (to
many) which has the employee id, effective date, billable hourly rate.
So in a given year calendar year they could have many different (though
usually 2 at most) rates. These rates then have to correspond to and e
multiplied by their corresponding billable hours from the time sheet
table.


Assuming that the timesheet table looks something like:

CREATE TABLE timesheets
(empid int NOT NULL,
day datetime NOT NULL,
billablehours tinynt NOT NULL)

You could do:

SELECT e.empname, SUM(c.rate * t.billablehours)
FROM employees e
JOIN compensations c1 ON e.empid = c1.empid
JOIN timesheets ON t.day >= c1.effective_date AND
t.day < (SELECT MIN(c2.effective_date)
FROM compensations c2
WHERE c2.empid = c1.empid
AND c2.effective_date > c1.effective_date)

This is a bit of a wild guess, and also untested. If this does not seme to
answer your problem, please include the following:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The expected output given the sample data.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Paul Rubin | last post by:
I frequently find myself writing stuff like # compute frob function, x has to be nonnegative x = read_input_data() assert x >= 0, x # mis-use of "assert" statement frob = sqrt(x)...
1
by: FuRiOuS1 | last post by:
Hi, i need some help with a select statement, i tried many different MySQL fucntions and looked through the MySQL manual, and it didnt help me any. What i want to do is select all rows from a table...
3
by: Eddie | last post by:
Hi to all I am building a database to track project expenses information. My database has 4 main tables, Employees, Activity, project info and rates. We add a project and track activities on...
1
by: Zurghew | last post by:
Hello all, I have just created a database (partially) which will bill customers for work done. However I am stuck when it is comes to different rates for different works. REQUIRED: What I...
1
by: Derrick | last post by:
Is it possible to "Inline" VB code in a C# class? I need the Like statement, regular expressions are different enough that we would like two searching options, "Like" and Regexs. If not, just...
7
by: Altemir | last post by:
I want to create an UPDATE statement that updates a value in a table in database A based on the results of an inner join between tables each residing in deifferent databases. What is the correct...
3
by: john | last post by:
In a query's where clause I have: Transactie.Rentedatum>#1/15/2005# In stead of 2005 I would like the query to pick the year of the date the query is being run. I tried (variations of) the...
1
by: naughtybynature | last post by:
<html> <head> <title>Search Questions</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $query = '';
6
by: =?Utf-8?B?UmljaA==?= | last post by:
In C# you have If ... { { ... } } If you place the mouse cursor next to a bracket and press ctrl something
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.