If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Need Help Calculating an Average Salary

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-18-09, 09:43
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
Need Help Calculating an Average Salary

Hey guys,

I am trying to create a query that will calculate each individual's average salary. The tricky part is I only want to average each individual's top 3 highest salaries in the history table.

I came close with this query:

SELECT mbr_ssn_nbr,
Floor(AVG(mbr_sal_amt)) as average_salary
from dsnp.pr01_t_mbr_hist
group by mbr_ssn_nbr
order by average_salary desc
fetch first 3 rows only



The problem here is that this query takes an average of each individual's entire salary history, and then gives me the top three highest average salaries. I need to use the Fetch command to retrieve each individual's top 3 salaries and then average that amount.

I'm sure it will have something to do with sub-queries which I'm not that comfortable with yet. Can anyone point me in the right direction? Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-18-09, 10:03
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Consider using OLAP functions. Something like
Code:
select avg(...) over (partition by ssn_number order by month desc rows 2 preceding)
Reply With Quote
  #3 (permalink)  
Old 03-18-09, 12:46
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
I am completely new to the OLAP functions, and I can't get one to run without syntax errors. For example:

select mbr_ssn_nbr,
row_NUMBER() over (order by mbr_sal_amt desc) as test
from dsnp.pr01_t_mbr_hist
order by test


I get An unexpected token "<END-OF-STATEMENT>" was found following ""

What am I overlooking here?
Reply With Quote
  #4 (permalink)  
Old 03-18-09, 14:02
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Try "...order by 2" or use a subselect. You cannot refer to column aliases (the "test") in the same subselect.
Reply With Quote
  #5 (permalink)  
Old 03-18-09, 14:49
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What platform(OS) and DB2 version did you used?

Newer version(at least DB2 V9 for LUW) supports ORDER BY simple-column-name.

Quote:
simple-column-name
Usually identifies a column of the result table.
In this case, simple-column-name must be the column name of a named column in the select list.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On