{"id":208,"date":"2013-04-25T12:10:02","date_gmt":"2013-04-25T17:10:02","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=208"},"modified":"2013-04-25T12:10:02","modified_gmt":"2013-04-25T17:10:02","slug":"five-number-summary","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=208","title":{"rendered":"Five Number Summary"},"content":{"rendered":"<p>From Freenode: how do you generate a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Five-number_summary\">five number summary<\/a> in MySQL? There is no &#8220;median&#8221; <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/group-by-functions.html\">aggregate function<\/a> built in. You could do some clever things involving self joins or temporary tables, or build an aggregate UDF &#8211; see the comments section in the manual for those approaches.<\/p>\n<p>Here&#8217;s another way using a single query. Be sure to set <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/server-system-variables.html#sysvar_group_concat_max_len\">group_concat_max_len<\/a> high enough for your data, and since it relies on string manipulation, it&#8217;s probably not a good choice if your data is millions of rows.<\/p>\n<p>First, a helper function to get the Nth element of a comma-delimited string, just to make the query shorter:<\/p>\n<pre>CREATE FUNCTION LIST_ELEM(inString text, pos int) \r\nRETURNS TEXT DETERMINISTIC \r\nRETURN SUBSTRING_INDEX(SUBSTRING_INDEX(inString, ',', pos), ',', -1);\r\n<\/pre>\n<p>Now, fetching the min, max, median, and first and third quartiles (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Quartile\">computing method 2<\/a>) for each group:<\/p>\n<pre>SELECT \r\n\r\n  groupId,\r\n\r\n  GROUP_CONCAT(data ORDER BY data) AS dataSet,\r\n\r\n  MIN(data) AS min,\r\n\r\n  (\r\n    LIST_ELEM(GROUP_CONCAT(data ORDER BY data), CEIL(COUNT(*)\/4))\r\n    + LIST_ELEM(GROUP_CONCAT(data ORDER BY data), FLOOR(COUNT(*)\/4 + 1))\r\n  ) \/ 2 AS q1,\r\n\r\n  (\r\n    LIST_ELEM(GROUP_CONCAT(data ORDER BY data), CEIL(COUNT(*)\/2))\r\n    + LIST_ELEM(GROUP_CONCAT(data ORDER BY data), FLOOR(COUNT(*)\/2 + 1))\r\n  ) \/ 2 AS median,\r\n\r\n  (\r\n    LIST_ELEM(GROUP_CONCAT(data ORDER BY data DESC), CEIL(COUNT(*)\/4))\r\n    + LIST_ELEM(GROUP_CONCAT(data ORDER BY data DESC), FLOOR(COUNT(*)\/4 + 1))\r\n  ) \/ 2 AS q3,\r\n\r\n  MAX(data) AS max\r\n\r\nFROM t \r\nGROUP BY groupId;<\/pre>\n<pre>+---------+---------------------+------+------+--------+------+------+\r\n| groupId | dataSet             | min  | q1   | median | q3   | max  |\r\n+---------+---------------------+------+------+--------+------+------+\r\n|       1 | 0,0,1,2,13,27,61,63 |    0 |  0.5 |    7.5 |   44 |   63 |\r\n|       2 | 0,0,1,2,25          |    0 |    0 |      1 |    2 |   25 |\r\n+---------+---------------------+------+------+--------+------+------+<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>From Freenode: how do you generate a five number summary in MySQL? There is no &#8220;median&#8221; aggregate function built in. You could do some clever things involving self joins or temporary tables, or build an aggregate UDF &#8211; see the comments section in the manual for those approaches. Here&#8217;s another way using a single query. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-3m","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":19,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/227"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}