shlogg · Early preview
Judy @esproc_spl

SQL Vs SPL: Ordered Grouping In Database Tables

Database table describes payment cycles for multiple projects with regular & closing months. SQL uses window functions to identify payment months & calculate total amounts, but SPL offers a more straightforward approach.

A certain database table describes the payment cycle for multiple projects (IDs), with one payment cycle consisting of regular months and a closing month. The regular month only has the current month's amount but no invoice, Invoiced=0; The closing month includes both the current month's amount and the invoice, Invoiced=1.

Now we need to identify each payment month for each project and calculate the total amount for that payment cycle. Note that the grouping criteria and order of the payment cycle are related, that is, "when last month's Invoiced=1, start a new group", which is different from...