Median in one fell swoop

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Median in one fell swoop

Post by agibsonsw »

Hello again. Access 2007.

I built this SQL to determine a median salary (of which I was quite proud!):

Code: Select all

(SELECT Last(Salary) AS Middles 
FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary ASC) 
ORDER BY Last(Salary)) UNION ALL (SELECT Last(Salary)  AS Middles 
FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary DESC) 
ORDER BY Last(Salary) );
But I also need to average the two return values. Is it possible to wrap this in AVG() without a second query? I've had a few goes but no joy so far.

Andy.

P.S. I'm aware that Access will return duplicate values (i.e. more than 50%) but this shouldn't matter in terms of a median value.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Median in one fell swoop

Post by HansV »

SELECT Avg(Middles) As MedianSalary
FROM (SELECT Max(Salary) AS Middles FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary ASC)) UNION ALL (SELECT Min(Salary) FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary DESC))
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Median in one fell swoop

Post by agibsonsw »

HansV wrote:SELECT Avg(Middles) As MedianSalary
FROM (SELECT Max(Salary) AS Middles FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary ASC)) UNION ALL (SELECT Min(Salary) FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary DESC))
Thanks again! I'll try this latter but I'm sure it will work :cheers:. Don't know why I keep reverting to First/Last rather than Min/Max when I know they've caused me problems in the past.

Regards, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Median in one fell swoop

Post by agibsonsw »

HansV wrote:SELECT Avg(Middles) As MedianSalary
FROM (SELECT Max(Salary) AS Middles FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary ASC)) UNION ALL (SELECT Min(Salary) FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary DESC))
Oops, this still gives two figures..
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Median in one fell swoop

Post by HansV »

Sorry, incorrect nesting of parentheses.

SELECT Avg(Middles) As MedianSalary
FROM (SELECT Max(Salary) AS Middles FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary ASC) UNION ALL SELECT Min(Salary) FROM (SELECT TOP 50 PERCENT Salary FROM tblExcel ORDER BY Salary DESC))
Best wishes,
Hans

User avatar
John Gray
PlatinumLounger
Posts: 5406
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Median in one fell swoop

Post by John Gray »

You are fortunate you were never exposed to LISP...!
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Median in one fell swoop

Post by HansV »

Define: Lisp
Result: Lisp is a speech impediment...
Best wishes,
Hans