tag:blogger.com,1999:blog-833560420200767834.post1677597760314292343..comments2023-10-14T01:23:27.239-07:00Comments on What Matters To Me: Finding the median value in SQLBarry Brownhttp://www.blogger.com/profile/16142746865672301098noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-833560420200767834.post-39245165702359713822009-04-12T14:21:00.000-07:002009-04-12T14:21:00.000-07:00You've hit the nail on the head. I had origina...You've hit the nail on the head. I had originally shown this to my SQL class and it worked on the dataset we were working with. But some students found datasets where it didn't work, so we came up with the solution that you found: using abs() to calculate how close they are to the middle.<BR/><BR/>Your solution needs one minor tweak: In the first sub-query, use >= as you did, but in Barry Brownhttps://www.blogger.com/profile/16142746865672301098noreply@blogger.comtag:blogger.com,1999:blog-833560420200767834.post-65264079078111165322009-04-01T14:00:00.000-07:002009-04-01T14:00:00.000-07:00Interesting approach. Unfortunately, it returns no...Interesting approach. Unfortunately, it returns no result when the median is a common value. For example: <BR/><BR/>[0, 0, 0, 7, 7, 7, 7, 10]<BR/><BR/>In this case, you subtraction always yields -7, -2, or 5, none of which are between 0 and 1. <BR/><BR/>My solution to this problem is to sort the results by closeness to the middle, then use MySQL's "limit" constraint to keep only theUnknownhttps://www.blogger.com/profile/00164580767758928600noreply@blogger.com