Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Effective T-SQL for Median Expand / Collapse
Author
Message
Posted Thursday, August 1, 2013 8:49 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, August 4, 2014 9:29 PM
Points: 459, Visits: 428
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!
Post #1479982
Posted Thursday, August 1, 2013 9:16 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:17 PM
Points: 4,474, Visits: 6,407
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!!


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1479989
Posted Thursday, August 1, 2013 9:21 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, August 4, 2014 9:29 PM
Points: 459, Visits: 428
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.
Post #1479990
Posted Thursday, August 1, 2013 9:32 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:17 PM
Points: 4,474, Visits: 6,407
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
Post #1479999
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse