Premium problem79. Median Search Count From a Histogram

Hard Locked

search_histogram(searches_per_user, user_count)

Each row says how many users performed exactly that many searches. Return the median searches_per_user across all users, rounded to two decimals, without expanding the table into one row per user.

With an even number of users the median is the average of the two middle values, which may fall in the same bucket or in two different ones.

Build a running total of user_count and ask which bucket contains the middle rank. A bucket covers the ranks from previous_total + 1 to running_total.

Tables

search_histogram

searches_per_user  user_count
-----------------  ----------
1                  2
2                  3
5                  4
9                  1

Expected result

median_searches
---------------
3.50

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query