• 0
Login to follow this  
Elena

Median Calculation

Question

Hello all,

 

First off, thank you for all the assistance given to me by the folks here at the community.

 

I am hoping to pick your brains yet again:

 

Is there an easier way to calculate the Median with SQL 2008 (which is the version that I am in, based on the plan that I purchased with Caspio)?

 

Below is my code, which is not working:

WITH Cnt AS
(SELECT COUNT(*) AS cnt)
,RN AS
(SELECT AutoID, ROW_NUMBER() OVER (ORDER BY Annual_Salaries) AS myNum
FROM market_data
)
SELECT AVG(1.*Annual_Salaries) AS median
FROM RN JOIN Cnt
on myNum IN ((cnt+1)/2,(cnt+2)/2)

Any help will be put in the bank of 'never to be forgotten'.

 

Elena

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

1 answer to this question

  • 0
On ‎3‎/‎25‎/‎2016 at 7:08 PM, Elena said:

Is there an easier way to calculate the Median with SQL 2008 (which is the version that I am in, based on the plan that I purchased with Caspio)?

 

Hi Elena,

To the best of my knowledge MSSQL does not have a native Median() function that I am aware of.   You seem to basically be doing what you need to.  Sorting based on the field in question and then going to the middle row and taking that value.  If you are still having a problem with this I can mess around with it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Login to follow this