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 > Can Someone Tell Me What is Wrong with My Logic?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-09, 16:47
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
Can Someone Tell Me What is Wrong with My Logic?

Hey guys,

I have a salary table called T_MBR_HIST:


SSN# MBR_SAL_AMT
_____ ____________



My goal is to retrieve an average of the employee's top 3 salary amounts in their history. I was able to use the FETCH and sort commands to return the top 3 salaries, but my logic fails when I try to average the results.

Here is my query:

SELECT AVG(A.MBR.SAL_AMT) FROM


(SELECT MBR_SAL_AMT FROM
DSNP.PR01_T_MBR_HIST
WHERE mbr_SSN_NBR = 444554444
ORDER BY MBR_SAL_AMT DESC
FETCH FIRST 3 ROWS ONLY ) AS A



It appears that DB2 doesn't like the order command inside my derived table. Any suggestions? Thanks!

Last edited by rockdave35; 02-26-09 at 16:28.
Reply With Quote
  #2 (permalink)  
Old 02-24-09, 17:29
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Use OLAP functions, something like "...avg(sal_amt) over (partition by emp_id order by sal_amt desc rows 2 following)..."

Not tested - use your imagination.
Reply With Quote
  #3 (permalink)  
Old 02-24-09, 19:22
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Which version of DB2 are you using on which platform? Older versions of DB2 LUW didn't support ORDER BY in a subselect - newer versions do.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 02-24-09, 19:53
tataforums tataforums is offline
Registered User
 
Join Date: Feb 2009
Posts: 9
ya. I agree with Stolze. V9 Supports orer by in Subqueries too.
Reply With Quote
  #5 (permalink)  
Old 02-24-09, 23:55
nick.ncs nick.ncs is offline
Registered User
 
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
Quote:
Originally Posted by rockdave35
It appears that DB2 doesn't like the order command inside my derived table. Any suggestions? Thanks!
Mind posting what error you get in the exact terms as displayed by DB2....
Is MBR_SAL_AMT numeric???
__________________
IBM Certified Database Associate, DB2 9 for LUW
Reply With Quote
  #6 (permalink)  
Old 02-25-09, 14:52
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by nick.ncs
Mind posting what error you get in the exact terms as displayed by DB2....
Is MBR_SAL_AMT numeric???
Here is the error:

The use of the reserved word "ORDER" following "" is not valid.
Expected tokens may include: ") UNION EXCEPT". SQLSTATE=42601

SQL0199N The use of the reserved word "ORDER" following "" is not valid. Expected tokens may include: ") UNION EXCEPT


I believe that I am on DB2 8, but not sure.
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