Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Effective T-SQL for Median


Effective T-SQL for Median

Author
Message
dkschill
dkschill
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 430
Stefan_G (8/1/2013)
The original query can be formulated a little bit more elegantly as this:

;WITH t(i,n,c) AS
( SELECT i,
ROW_NUMBER() OVER (ORDER BY i),
COUNT(*) OVER (PARTITION BY (SELECT NULL))
FROM #T)
SELECT
AVG(i) AS Median
FROM t
WHERE 2*n-c BETWEEN 0 AND 2;



This version should be slightly faster because of the simpler expression in the WHERE clause

/SG


Awesome! I enjoy math; so proving this to myself was a lot of fun. Thanks for the new trick!
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5998 Visits: 8314
Alan.B (7/31/2013)
TheSQLGuru (7/30/2013)
Fortunately for you SQL 2012 has much better Windowing Function support. Look into the PERCENTILE_CONT function. I also HIGHLY recommend you purchase Itzik Ben-Gan's SQL Server 2012 High-Performance TSQL Using Window Functions book.


Just an interesting FYI...

I have never used PERCENTILE_CONT but I do have Server 2012 High-Performance TSQL Using Window Functions. With the same sample data from earlier (#T), this is a modified version of Ben-Gan's 2012-based solution using PERCENTILE_COUNT:


WITH t(r,m) AS
( SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)),
PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY i) OVER(PARTITION BY NULL)
FROM #T
)
SELECT m AS median
FROM t
WHERE r=1;



Though it is much simpler, Ben-Gan's 2012-PERCENTILE_CONT solution is about twice as slow as any other solution posted thus far. Again, just an FYI.


Yep, but the book covers other non-2012 solutions for median as well (and has lots of other great stuff in it) so it was definitely worth the recommendation.

I will add that it is quite amazing how varied solutions to the same problem can be achieved in TSQL, and just as amazing how varied the performance characteristics of those solutions can be!! w00t

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
dkschill
dkschill
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 430
One of the main reasons I love to post here! I will have to check out that book...it was mentioned a number of times yesterday during the 24 Hours of PASS.
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5998 Visits: 8314
dkschill (8/1/2013)
One of the main reasons I love to post here! I will have to check out that book...it was mentioned a number of taps yesterday during the 24 Hours of PASS.


Personally I think ANYTHING written by Itzik is worth reading and/or having in your SQL Server library! :-)

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search