Who Needs Graphics? Create Charts in SQL

By | | Web Tech

9

SQL pie chartThis is one of the most impressive demonstrations of SQL I’ve seen. Shlomi Noach, a database consultant from Israel, has developed a single SQL query that analyzes a table and generates a pie chart in ASCII art.

Shlomi’s blog post, SQL pie chart, provides a full step-by-step analysis of how it was achieved. In summary:

  1. Values from a single table are examined and accumulating totals are calculated.
  2. From the accumulating totals, accumulating ratios are calculated in a 0 to 1 range and 0 to 2 * PI range (radians in a whole circle).
  3. A co-ordinates table is then created. This starts at zero in the top-left and increases in appropriate values to a maximum size in the bottom right.
  4. The co-ordinates table is used to generate a table of degrees in relation to the center of the table.
  5. The original table data is then mapped on to the degrees table, assigned an ASCII character for each ‘color’, and anything outside the circle is set to a space.
  6. Finally, a legend is added.
+----------------------------------------------------------------------------------------------+
| pie chart                                                                                    |
+----------------------------------------------------------------------------------------------+
|                                                                                              |
|                                   ;;;;;;;;;;;;;;;;;;;;;;;;;                                  |
|                          oooooooo;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                         |
|                    ooooooooooooooo;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                   |
|                oooooooooooooooooooo;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;               |
|             oooooooooooooooooooooooo                     ;;;;;;;;;;;;;;;;;;;;;###            |
|           oooooooooooooooooooo                                 ;;;;;;;;;###########          |
|         oooooooooooooooooo                                         ##################        |
|       ooooooooooooooooo                                               #################      |
|      ooooooooooooooooo                                                 #################     |
|      oooooooooooooooo                                                   ################     |
|     oooooooooooooooo                                                     ################    |
|      oooooooooooooooo                                                   ::::::::::::::::     |
|      ooooooooooooooooo                                                 :::::::::::::::::     |
|       ooooooooooooooooo                                               :::::::::::::::::      |
|         oooooooooooooo::::                                         ::::::::::::::::::        |
|           ooooooo:::::::::::::                                 ::::::::::::::::::::          |
|             ::::::::::::::::::::::::                     ::::::::::::::::::::::::            |
|                :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::               |
|                    :::::::::::::::::::::::::::::::::::::::::::::::::::::::                   |
|                          :::::::::::::::::::::::::::::::::::::::::::                         |
|                                   :::::::::::::::::::::::::                                  |
|                                                                                              |
| ##  red: 1 (10%)                                                                             |
| ;;  blue: 2 (20%)                                                                            |
| oo  orange: 3 (30%)                                                                          |
| ::  white: 4 (40%)                                                                           |
+----------------------------------------------------------------------------------------------+

As you might imagine, the resulting query is a little long and complex. I won’t reproduce it here but Shlomi’s post provides the code if you want to try it for yourself.

It’s very clever. And utterly mad.

Have you seen any other examples of bizarre SQL queries? Post your links below…

Written By:

Craig Buckler

Craig is a Director of OptimalWorks, a UK consultancy dedicated to building award-winning websites implementing standards, accessibility, SEO, and best-practice techniques.

Website
>> More Posts By Craig Buckler

 

{ 9 comments }

Shlomi Noach August 18, 2009 at 10:52 pm

Hey,

Thanks for the mention!
Just in response to Mike’s bar chart example, why don’t you also have a look at my more elaborate graphs:
http://code.openark.org/blog/mysql/auto-scaling-scaled-sql-graphs-concluded

Cheers,
Shlomi

hallodom August 17, 2009 at 11:29 pm

Am staying with Flash!

Shonkyboy August 17, 2009 at 5:56 am

Great – but utterly pointless and a huge waste of time – pretty much like the 7 hours fishing i did today :)

Mike August 16, 2009 at 7:55 pm
Dr John August 16, 2009 at 7:09 pm

and just the sort of query we can regularly make use of as well.
;)

Houdas August 16, 2009 at 6:44 pm

Madness?

This… is… SQLPARTA!

cts91 August 16, 2009 at 1:28 am

What a BEAST! I’m in awe.

SilverBulletUK August 16, 2009 at 12:53 am

I fear Rudy’s response! :-)

Dan Grossman August 16, 2009 at 12:40 am

I absolutely love it! Genius!

Comments on this entry are closed.