Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 8th, 2003, 11:57 AM
suman suman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 suman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
General SQL Question

Hey guys,

Maybe one of you guys can help me out figuring this SQL delimma. Here's the deal :

I have 3 tables (Employee, PayGroup and Work_Summary).

Employee
------------
Emp_Id (PK)
Loc_Id


PayGroup
------------
Paygroup_Id (PK)
PayGrp_Name (FK to Loc_Id in Employee table)
End_Date


Work_Summary
--------------------
Wrks_Id (PK)
Emp_Id (FK to Emp_Id in Employee table)
Wrks_Work_Date
Wrks_Base_Rate

The Paygroup table has one record per employee
The Work_Summary table has multiple records per employee.

The task is to generate a report as such :

Emp_Id Rate1 Rate2 Rate3 Rate4 Rate 5 Avg
-----------------------------------------------------------------------------
1000 20.200 21.200 25.200 21.000 19.000 avg val
........
........

This is how the 'rate1, rate2' values are to be calculated :

1) For every employee, start off by matching the LOC_ID in the employee table to the PayGrp_Name in the Paygroup table. This gives you one and only one record.

2) Pick up the End_Date from the Paygroup table for this employee and substract 7 from this date (basically go back a week).

3) Match this date to the Wrks_Work_Date column from the Work_Summary table based on the employee's Id and look up the value in Wrks_Base_Rate column. This gives you the value for 'Rate5'.

Then, using the same End_Date value that was used earlier in step 2), go back 14 days, and do the same as mentioned above in Step 3). This gives you 'Rate 4'.

Similarly, 'Rate 3' is minus 21 days, 'Rate 2' is minus 28 days and 'Rate 1' is minus 35 days.

But they all need to appear on the same line in the report.

And then the average that is calculated in the last column is the total of all NON-ZERO 'Rate' values divided by the number of non-zero rates. Eg :

Rate1 Rate2 Rate3 Rate4 Rate5
10.05 20.50 10.00 0.00 0.00

In this case, the avg is (10.05 + 20.50 + 10.00)/3
and NOT (10.05 + 20.50 + 10.00 + 0.00 + 0.00)/5

This is one complex SQL to me !! )

Anyone that can help me out and teach me a few things in the process ??

Thanks in advance for your help !

Reply With Quote
  #2  
Old September 8th, 2003, 01:11 PM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 692 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 21 h 22 m 10 sec
Reputation Power: 19
You didn't say what DB, this is how it could be done with Oracle. The rate's are easy but getting the avg is a little tricky. The decode is like an inline if that I am using to come up with the denominator for the avg calculation. You should be able to find the equivilent for your db:

PHP Code:
 select e.emp_idws1.wrks_base_rate as rate1ws2.wrks_base_rate as rate2ws3.wrks_base_rate as rate3,
       
ws4.wrks_base_rate as rate4ws5.wrks_base_rate as rate5,
       (
ws1.wrks_base_rate +
        
ws2.wrks_base_rate +
        
ws3.wrks_base_rate +
        
ws4.wrks_base_rate +
        
ws5.wrks_base_rate /
        
decode(ws1.wrks_base_rate,0,0,1) +
        
decode(ws2.wrks_base_rate,0,0,1) +
        
decode(ws3.wrks_base_rate,0,0,1) +
        
decode(ws4.wrks_base_rate,0,0,1) +
        
decode(ws5.wrks_base_rate,0,0,1)
       ) 
avgrate
  from employee e
paygroup pgwork_summary as ws1work_summary as ws2work_summary as ws3
       
work_summary as ws4work_summary as ws5
 where e
.loc_id pg.paygrp_name
   
and pg.end_date-35 ws1.wrks_wrk_date
   
and pg.end_date-28 ws2.wrks_wrk_date
   
and pg.end_date-21 ws3.wrks_wrk_date
   
and pg.end_date-14 ws4.wrks_wrk_date
   
and pg.end_date-7   ws5.wrks_wrk_date

Reply With Quote
  #3  
Old September 8th, 2003, 01:30 PM
suman suman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 suman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To Hedge

Dude,

Thanks for the reply. Really appreciate it. A few questions/comments :

1) What is the decode trying to do ?

2) Also, when I run the SQL, it shows the rates 1 thru 5, but the average is not being computed correctly. (if all but one rate has a value, the average is still showing up as 0)

3) If I want the rates 1 thru 5 to be rounded UP to the nearest half cent, is there a function to do that ?

Thanks again !
S

Reply With Quote
  #4  
Old September 8th, 2003, 01:48 PM
suman suman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 suman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To Hedge (again)

Hedge,

Also, should I not be joining the emp table to the work_summary table in your SQL using something like this in the where clause :

where ws1.emp_id = e.emp_id
and ws2.emp_id = e.emp_id
and ws3.emp_id = e.emp_id
and ws4.emp_id = e.emp_id
and ws5.emp_id = e.emp_id

Reply With Quote
  #5  
Old September 8th, 2003, 02:20 PM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 692 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 21 h 22 m 10 sec
Reputation Power: 19
Re: To Hedge

Quote:
Originally posted by suman
Dude,

Thanks for the reply. Really appreciate it. A few questions/comments :

1) What is the decode trying to do ?

2) Also, when I run the SQL, it shows the rates 1 thru 5, but the average is not being computed correctly. (if all but one rate has a value, the average is still showing up as 0)

3) If I want the rates 1 thru 5 to be rounded UP to the nearest half cent, is there a function to do that ?

Thanks again !
S


1. The decode is trying to come up with the denominator by doing a test on each rate, if it is 0 then return 0 otherwise return 1... by adding these up we should be able to get the correct denominator.

2. I looked at it again and it seems to me that it should work, maybe split the query to show the numerator and denominator seperately to see which one is wrong.

3. yes, look at the round function

Reply With Quote
  #6  
Old September 8th, 2003, 02:20 PM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 692 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 21 h 22 m 10 sec
Reputation Power: 19
Re: To Hedge (again)

Quote:
Originally posted by suman
Hedge,

Also, should I not be joining the emp table to the work_summary table in your SQL using something like this in the where clause :

where ws1.emp_id = e.emp_id
and ws2.emp_id = e.emp_id
and ws3.emp_id = e.emp_id
and ws4.emp_id = e.emp_id
and ws5.emp_id = e.emp_id


Yep, It looks like I missed the correct join

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > General SQL Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway