Skip to main content

Some subquery magic with MySQL

As part of a Rails project I had to gather up some data from a single table, and calculate various stats on the data

Problem was - had to group data by a field, then calculate aggregates in various ways not possible with a single GROUP BY query

Using the SQL cookbook tips, and some pondering, I learned that I could create a bunch of 'fake' tables from the same table using subqueries, then join these tables in a way that would yield a single resultset

An example:

Let us say you have a table of person, their test points whether they passed the test attempt, like (two people a and b tried a test at various times) :












NamePointsPassed
a10 1
a25 0
b21 1
a28 0
b23 1
a35 1
b31 1
a35 1
b41 1


You want a result like





Name Avg AgeNo. of attempts'passed'
a 30.3 5
b 25.1 4
(caution: These numbers are probably wrong just too lazy to fix them up!)

As you can see, it is not possible with a single query, since grouping by name and aggregating for average will give you column 2 of the results, but how about column 3? column 3 is the no. of records which indicate a 1 for passed for each Name

To achieve this in a single query, do the following


Create a subquery to hold the average age results

SELECT avg(Age) as avgage from table a GROUP BY Name


Create a subquery to hold the no. of passed points per Name

SELECT count(0) as passed FROM table WHERE Passed=1 GROUP BY Name

Now join the two subqueries
SELECT t.Name, q1.avgage, q2.passed FROM table t
LEFT JOIN
( SELECT avg(Age) as avgage from table a GROUP BY Name ) q1
ON q1.Name=t.Name

LEFT JOIN
( SELECT count(0) as passed FROM table WHERE Passed=1 GROUP BY Name ) q2
ON q2.Name=t.Name

ORDER BY Name

the left join is not obvious in this case, but you could end up with missed matchse in more complex queries (then it is useful to use IFNULL() MySQL function to detect nulls and set values other than null in those cases)

An example of a real query is :


SELECT xx.label, xx.avg_time, xx.min_time, xx.max_time, xx.std_time,
IFNULL(yy.cnt_s,0) AS s,IFNULL(zz.cnt_f,0) AS f,
(IFNULL(yy.cnt_s,0) + IFNULL(zz.cnt_f,0)) AS totalsamples
FROM
# table 1, averages
( SELECT label,
AVG(responseTime) AS avg_time,
MIN(responseTime) AS min_time,
MAX(responseTime) AS max_time,
STD(responseTime) AS std_time
FROM rawdatas
WHERE jmetersession_id = 1
GROUP BY label ) xx
LEFT JOIN
# table 2, successes
(SELECT b.label, COUNT(0) AS cnt_s FROM rawdatas b
WHERE b.jmetersession_id = 1 AND b.success=1 GROUP BY b.label ) yy
ON yy.label=xx.label
# table 3, failures
LEFT JOIN
(SELECT label, COUNT(0) AS cnt_f FROM rawdatas
WHERE jmetersession_id = 1 AND success=0 GROUP BY label ) zz
ON zz.label=xx.label


Hope you find this useful!

Comments

Popular posts from this blog

Unit testing code that uses environment variables and system properties with fakes

I did not exactly learn this today, but I am sharing it as I have found it extremely useful when unit testing code that depends on environment or system property settings. While I am using Java as an example, the general concepts apply any where. Problem : You have a piece of code you are unit testing that uses settings from env variables or system properties passed to the VM (System.getProperty), but you don't want the tests to be affected by the 'real' environment or system properties in the VM. So, your unit tests should not get different results or fail when the real environment changes. Solution : There are several. But the most straightforward is to use a mocking library to mock out the environment or fake it out, whatever your prefer. You can create a fake using a library like EasyMock, PowerMock etc. This I won't discuss in this post, since there are numerous articles for that. Or you can write a simple class that acts as a proxy, using the proxy pattern...

Sending Form data to a backend REST API using Axios

This need is incredibly common and useful, and hopefully will save you a lot of time when doing server side calls from your UI application (or even non UI clients like NodeJS applications) Example here is to send a POST request to an endoint /api/item/new (which will create a new item in the database). We will just assume tbhe backend is already setup (it's not relevant to this article). All we need to know is that we can do a POST /api/item/new and send it form data with two pieces of info     name, filter So, if you have a node.js application (I was using Vue-cli generated project, but it does not matter), install 'axios' (a most popular tool to make server calls these days) npm i axios --save OR yarn add axios (my preferred method) Now, in your service JS file (which is generally when I keep all my api calls) do something like this createNew ( name , filter ) { let formData = new FormData (); formData . append ( "name" , ...

CSS: em vs rem font sizes

 When do you use em and when do you use rem? If you have ever asked this, you are like me :) So welcome. Basically, to save you time here it is: - If you want your font-size relative to the container's font-size, use em - If you want your font-size relative to the 'root' (or html) element's font-size, use rem! If you just stop reading now that might be sufficient, but if you are more curious, go on. Example companion codepen: https://codepen.io/binodpanta/pen/RwLWRra Basically your page should ideally always have a default font-size specified for the root, such as  :root { font-size: 1em; } This typically becomes 16px default for the base font size. Now, if you use rems in your elements' styles you get a consistent scaling wrt this number! so if you do div.someclass { font-size: 0.5rem; } you are going to always get a nice scaled font size regardless of screen size. So all your fonts will scale relatively throughout the app!  If you had used 0.5em, your calculated ...