Skip to content
Advertisement

Mysql query : how to calculate statistic growth rate between two period per category

From this table of dates and categories inputs :

enter image description here

I’d like to get these folowing table, showing number of rows per category for each first and second quarter, and the growth of number of rows between second and first quarter, in value, and in percentage – which is a simple statistic table type that could be met to get the number of item per period of time and its growth.

enter image description here

What would be the sql query to get this table ?

Here is the SQL code to create the sql table, in order you to reproduce the schema:

Here is the sql code to fill the table with the data :

I’ve tried the following sql code, to get the result table, but I do not know where I should, and how I should introduce the category field in order to get a group by output.

The above code, returns the following :

enter image description here

So now, I’d like t place the category field into the code. But I do not knwo how to do it.

Any idea ?

Advertisement

Answer

You would need to aggregate by categories within your subqueries first so you wouldn’t lose the category details in the final projection. See a sample working fiddle and results below:


Query #1

category number of row in q1 number of row in q2 variation of nb of row q2 vs q1 growth nb of rows between q2 vs q1
A 2 1 -1 -50.0000
B 1 2 1 100.0000
C 1 1 0 0.0000

View on DB Fiddle

Update 1

Another approach has been included below where the aggregation has been done with the help of a case expression. I have also added to your test data, D only in q2, E only in q1 and F neither in q1 or q2. This approach includes categories in either quarter. I also added a case expression for categories that are new or only occurring in quarter 2 that would have the growth rate as null. This is up to you whether you would like the growth rate returned as null or a default value, I included 100


Query #1

category number of row in q1 number of row in q2 variation of nb of row q2 vs q1 growth nb of rows between q2 vs q1
A 2 1 -1 -50.0000
B 1 2 1 100.0000
C 1 1 0 0.0000
D 0 1 1 100.0
E 1 0 -1 -100.0000

View on DB Fiddle

Feel free to experiment with other filtering options. For example, if you would like to filter out entries like D or E that exist in only one quarter you may consider adding a WHERE clause similar to WHERE q1 > 0 and q2 > 0; as shown below:

category number of row in q1 number of row in q2 variation of nb of row q2 vs q1 growth nb of rows between q2 vs q1
A 2 1 -1 -50.0000
B 1 2 1 100.0000
C 1 1 0 0.0000

View on DB Fiddle

I hope this helps.

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