Skip to content
Advertisement

Mysql query per date and per group

I have an SQL query which counts user uptake (registrations) by day for a particular group of users. It also does a cumulative (running total)

This query is using the audit table purely to get me a list of dates reliable even if there are no users created on every day, i know the audit table with have a record.

I could get all the groups easily with

What i want is… An entry per day and group with the uptake (and cumulative if possible) Example:

Advertisement

Answer

For versions of MySQL before 8.0, we could make use of user-defined variables, as demonstrated in the OP query.

A few notes:

It looks like we started with a simple GROUP BY created_date, group_uuid query against just the users table to get non-zero counts. But that’s got missing rows, where there otherwise be a zero count.

So it looks like we need is a calendar source (a distinct list of dates in a given range, OP query is using query of audit table as calendar source) and we need that cross joined (Cartesian product) to a distinct list of uuid values from group, along with the name. For this exercise, we are going to assume that uuid is unique in the groups table, and that each uuid value is associated with a distinct name value. (If that’s not true, we’d need to make some adjustments.)

To get the running total separately by group, we’d need to process rows in group order, then within each group, by ascending date order. As we process rows, we need to reset the running total to zero when we start a new group i.e. when detect a change in the group uuid value.

NOTE: The MySQL Reference Manual gives specific warning about using the user-defined variables, both reading and modifying in the same statement, the order of operations is not guaranteed. Up through MySQL 5.7, with carefully crafted queries, we do observe predictable behavior in the order of operations.

So, we could do something like this:

N.B. We have to be careful with the order of operations in the SELECT list that’s using the user-defined variables, for example, we need to test the value of @prev_uuid (saved from the previous row) before we overwrite it with the value of the current row.

Also, this is not tested, there could be syntax errors, missing parens. I strongly recommend testing each of the inline view queries separately (cal, grp, cnt) to verify expected results are being performed. (The WHERE clause in the grp query is restricting groups to be returned, adjust or eliminate as required.)

Then we can move on to testing the next outer query q, and confirm the results from that, before wrapping q in the final outermost query to re-order rows, and display columns in desired order.

** FOLLOWUP **

to see if we can get MySQL to honor the ORDER BY uuid , we can modify/replace q

Change from this:

to

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement