Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Median in MS Access

Status
Not open for further replies.

Piotr_M

Programmer
Jun 16, 2020
1
PL
thread701-1707625

Hello everyone!
I found that closed thread with solution on making median calculation using MS Access.
It's a brilliant conception there, but leads to performance disaster, when we have little more records in data set (100k and more for example).
I redesigned the query, adding nested level and replacing functions Min and Max with Last, which totally improved performance. Just wanted to share. Cheers!
P.S.
The closed thread was focused on developing the concept into VBA function. I focused on conception itself, showing SQL only.



Originally posted query:

Select
Max(Max(X.ClosingPrice) + Min(Y.closingPrice))/2 AS Median
from
(Select TOP 50 PERCENT closingPrice from tblHomes ORDER BY ClosingPrice) AS X,
(Select TOP 50 PERCENT closingPrice from tblHomes ORDER BY ClosingPrice DESC) as Y


...and after my improvement:

Select
(X.closingPrice_1 + Y.closingPrice_2)/2 AS Median
from
(SELECT LAST(closingPrice) AS closingPrice_1 FROM (Select TOP 50 PERCENT closingPrice from tblHomes ORDER BY ClosingPrice)) AS X,
(SELECT LAST(closingPrice) AS closingPrice_2 FROM (Select TOP 50 PERCENT closingPrice from tblHomes ORDER BY ClosingPrice DESC)) as Y

 
Thanks for sharing and welcome to TT

Pozdrowionka... [wavey3]


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top