Solved

need to get an average

Posted on 2013-02-01
4
191 Views
Last Modified: 2013-02-04
I am trying to get the average pay for a group of people, but the way the database is setup everytime someone gets a raise it is a new entry, so someone could have 1 entry or 20.  This is screwing up my average because it is taking the average of what they originally got paid through what they are making now and I just need their latest pay rate.

For instance this is what I see

Joe Mc  $8
Joe Mc  $8.50
Joe Mc  $10
John Cour  $9

Average = $8.88 where it should be $9.50.  Joe should only count the one time.
0
Comment
Question by:TomBalla
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 430 total points
ID: 38843882
so, you have to first get the max() value for each, and then average on those, something along this sql code:
select average( max_pay)
  from ( select username, max(pay) as max_pay )
                from yourtable
            group by username
     ) subquery 

Open in new window


the code may need to be varied base on the  db engine

max(pay) may be wrong, if a user could also go "down" on the pay ...
in that case, you may need to consider other techniques, to get the max (or current) date for the pay to consider.
see this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 38845882
Do you have a date field that indicates when the pay change occurred?

If so you could use that to find the current pay.

Same basic query

select average(pay)
  from ( select username, pay, Max(paydate) as max_paydate )
                from yourtable
            group by username
     ) subquery 

Open in new window


mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 38846592
mlmcc,

 I don't think that will work.  The sub-query will include each different pay rate, so you're still getting an average for all of the rates.  It will include the maximum date with each rate, but that doesn't help (if anything, it makes things more confusing, since all of the rates now have the same date).  I think you need another level.  At the bottom level, find the maximum date, then find the rate for that date, then do the average.


 Also, FWIW, in your query and angelIII's, the ")" should not be at the end of the second line.

 James
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 38847002
YOu are right.  Need to check for max date in the query.

mlmcc
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question