Created
June 13, 2014 16:09
-
-
Save napsternxg/01aa816277e55141406b to your computer and use it in GitHub Desktop.
MySQL: Getting data for histogram plot? Source: http://stackoverflow.com/questions/1764881/mysql-getting-data-for-histogram-plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT ROUND(numeric_value, -2) AS bucket, | |
COUNT(*) AS COUNT, | |
RPAD('', LOG(COUNT(*)), '*') AS bar | |
FROM my_table | |
GROUP BY bucket; | |
/* | |
numeric_value = column name | |
-2 to round in steps of 100. Use -1 for steps of 10 and any positive vale for rounding to nearest decimal | |
LOG(COUNT(*)) is to make the count on log scale | |
Output: | |
+--------+----------+-----------------+ | |
| bucket | count | bar | | |
+--------+----------+-----------------+ | |
| -500 | 1 | | | |
| -400 | 2 | * | | |
| -300 | 2 | * | | |
| -200 | 9 | ** | | |
| -100 | 52 | **** | | |
| 0 | 5310766 | *************** | | |
| 100 | 20779 | ********** | | |
| 200 | 1865 | ******** | | |
| 300 | 527 | ****** | | |
| 400 | 170 | ***** | | |
| 500 | 79 | **** | | |
| 600 | 63 | **** | | |
| 700 | 35 | **** | | |
| 800 | 14 | *** | | |
| 900 | 15 | *** | | |
| 1000 | 6 | ** | | |
| 1100 | 7 | ** | | |
| 1200 | 8 | ** | | |
| 1300 | 5 | ** | | |
| 1400 | 2 | * | | |
| 1500 | 4 | * | | |
+--------+----------+-----------------+ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment