software:mysql:withrecursive
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| software:mysql:withrecursive [2026/02/06 21:47] – rodolico | software:mysql:withrecursive [2026/02/06 22:05] (current) – rodolico | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== The WITH RECURSIVE syntax ====== | ====== The WITH RECURSIVE syntax ====== | ||
| - | Using FrontAccounting (https:// | + | Using [[https:// |
| - | The basic query, after some looking around, turned out to be: | + | ===== Warnings ===== |
| + | |||
| + | Just like it says, this is recursion, generally used to retrieve data from a hierarchical configuration. | ||
| + | |||
| + | It is very resource intensive if it has to recurse very deep. Be careful when implementing this in the real world, as a poorly written CTE can blow up your database server. | ||
| + | |||
| + | ===== Original Task ===== | ||
| + | |||
| + | After some looking around, | ||
| <code sql> | <code sql> | ||
| Line 23: | Line 31: | ||
| </ | </ | ||
| - | I then ran the output of this through a Perl script | + | I then ran the output of this through a Perl script |
| - | I was able to bypass the Perl script by learning the new CTE (Common Table Expressions) syntax for MySQL (and MariaDB). This is similar to processing queries into temp tables, then running an additional query against them. | + | I was able to bypass the Perl script by learning the CTE (Common Table Expressions) syntax for MySQL (and MariaDB). This is similar to processing queries into temp tables, then running an additional query against them. |
| + | |||
| + | ===== Solution ===== | ||
| I'll say more about it later, but here is the resulting query. | I'll say more about it later, but here is the resulting query. | ||
| Line 93: | Line 103: | ||
| </ | </ | ||
| - | | + | * This projects 12 months |
| - | * Our invoicing is done on the 15th of every month, so in three locations, we have that hard coded also. Look for //' | + | |
| - | * | + | |
| + | ===== Explanation ===== | ||
| + | |||
| + | |||
| + | ==== Purpose ==== | ||
| + | The query aggregates recurring invoice totals by month. Each invoice recurs every '' | ||
| + | |||
| + | ==== Query Structure ==== | ||
| + | The query uses three CTEs: | ||
| + | |||
| + | 1. **'' | ||
| + | * Builds a normalized recurring-invoice dataset from FrontAccounting tables. | ||
| + | * Computes: | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | 2. **'' | ||
| + | * Calculates the reporting window: | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | 3. **'' | ||
| + | * Generates a row per calendar month from '' | ||
| + | |||
| + | ==== Final Aggregation ==== | ||
| + | For each generated '' | ||
| + | |||
| + | * A row is billed when the difference in months between '' | ||
| + | * This is evaluated with: | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ==== Output ==== | ||
| + | The result returns one row per month: | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ==== Adjusting the Reporting Range ==== | ||
| + | To change the number of months produced, edit the interval in the '' | ||
| + | |||
| + | <code sql> | ||
| + | DATE_ADD(DATE_FORMAT(MIN(nextDue), | ||
| + | </ | ||
| + | |||
| + | For example, replace '' | ||
| + | |||
| + | ==== Notes ==== | ||
| + | * The query is compatible with MariaDB 10.11. | ||
| + | * If there are no '' | ||
| + | |||
software/mysql/withrecursive.1770436021.txt.gz · Last modified: by rodolico
