473,503 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2017
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
1759
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
6378
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
1596
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
1327
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
1570
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
24669
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
2030
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
2011
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
1631
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
7199
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
7074
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
7273
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
7322
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
7451
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...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.