Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Effective T-SQL for Median Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, August 01, 2013 8:49 AM
 SSC-Addicted Group: General Forum Members Last Login: 2 days ago @ 8:22 AM Points: 456, Visits: 414
 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) SELECTAVG(i) AS MedianFROM tWHERE 2*n-c BETWEEN 0 AND 2;`This version should be slightly faster because of the simpler expression in the WHERE clause/SGAwesome! I enjoy math; so proving this to myself was a lot of fun. Thanks for the new trick!
Post #1479982
 Posted Thursday, August 01, 2013 9:16 AM
 SSCarpal Tunnel Group: General Forum Members Last Login: Today @ 2:04 PM Points: 4,082, Visits: 5,745
 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 medianFROM tWHERE 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. BolesSQL Server ConsultantSQL MVP 2007-2012TheSQLGuru at GMail
Post #1479989
 Posted Thursday, August 01, 2013 9:21 AM
 SSC-Addicted Group: General Forum Members Last Login: 2 days ago @ 8:22 AM Points: 456, Visits: 414
 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 01, 2013 9:32 AM
 SSCarpal Tunnel Group: General Forum Members Last Login: Today @ 2:04 PM Points: 4,082, Visits: 5,745
 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. BolesSQL Server ConsultantSQL MVP 2007-2012TheSQLGuru at GMail
Post #1479999

 Permissions