Wanted to share an sql function I end up using quite frequently when dealing with user input data – normalization to alphanumeric. It can also be modified into a function to extract numeric values. If you do modify it to that, make sure you keep returns varchar for the cases of extracting numeric values beginning with 0. You can also add other characters, such as the minus sign, if required.
I recently created a job that calculates some KPIs and writes a txt file that gets picked up as an email body for daily delivery. It was a few lines of text, with the date and kpi value, like the example below
2014-01-16 New Customers: 7 Sold_Items: 15
2014-01-15 New Customers: 6 Sold_Items: 12
2014-01-14 New Customers: 5 Sold_Items: 6
2014-01-13 New Customers: 4 Sold_Items: 8
2014-01-12 New Customers: 3 Sold_Items: 8
2014-01-11 New Customers: 2 Sold_Items: 4
2014-01-10 New Customers: 1 Sold_Items: 3
It was the easiest approach to just concatenate date, customers, items coming from my data source with some strings, but of course it was hard to read. There were several such blocks of text in it, so while the data was there, it was hard to see a trend.
the sql for it went something like:
SELECT concat_ws(' ', date, ' New Customers:', users, ' Sold_Items:', items)
from kpi_table
ORDER BY date DESC
LIMIT 7
INTO OUTFILE 'C:\\folder\\email_kpi.txt' FIELDS TERMINATED BY ';';
Easy, but by far not the best way to deliver an email report.
To properly display this data with the idea in mind that the recipient will have to read and understand it, it must be represented differently.
Best ways are a visual aid, along with a structured format to allow the user to work with the data, so I decided for a chart and a table. Luckily, google charts offers a simple way that allows you to put parameters in an url, which will then return the chart. So I had a go at it, and came up with below.
Note the url is not secure; on the other ahnd, this is not a problem, as you can only access the specific url with the data you put in it. In other words, if you don’t have the data in the first place, you can’t access it. It’s not dynamic, so there’s no possibility of future leaks.
As for the table, a simple html table will do – it allows you to copy paste straight into spreadsheets and keep the formatting, and the simpler the better. Table formatting may vary, it will normally look pretty plain, but some email clients support inline css, so if you have the time, feel free to experiment.
date
New Customers
Sold Items
2014-01-26
15
30
2014-01-25
20
40
2014-01-24
49
80
2014-01-23
45
73
2014-01-22
22
40
2014-01-21
21
50
2014-01-20
18
33
What about the code to create this every day?
I use plain mysql that generates the two lines as text
group_concat(date order by date asc separator'|'),
'|1:|',
concat_ws('|',
0,
floor(greatest(max(items),max(users))/50+1)*10,
floor(greatest(max(items),max(users))/50+1)*20,
floor(greatest(max(items),max(users))/50+1)*30,
floor(greatest(max(items),max(users))/50+1)*40,
floor(greatest(max(items),max(users))/50+1)*50
),/** y axis notch values dinamically according to higest chart value **/
'&chco=044444,099999&chg=16.67,20&chm=N,004040,0,-1,11|N,005050,1,-1,11&chtt=Sold_items',/** line color, numbers color, grid frequency - 20 is 5, 16.67 is 6; title **/
'" >'
)
from(
selectk.date,k.users,k.sized_items
from kpi_tablekorder by date desc limit7
)k
For the google image chart it’s a bit more work to get the vertical limit and the axes to update according to your maximum values, but otherwise pretty straightforward. The easiest way to figure out what you need is in the google playground in the link below. Alternatively you can just change the parameters in the url of the google chart above
You end up with just one line for each, perfect for shooting into a html email. Easy to read, looks good.
and the final result:
Stay tuned for more!
By continuing to use the site, you agree to the use of cookies. more information
The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.