Lädt...

🔧 Calculate monthly account balance and fill in missing dates:From SQL to SPL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

There is a transaction table for the asset accounts in the MS SQL database, with dates that are not consecutive.

Image description
Now we need to calculate the balance of each account for each month from January 2021 at the beginning of the period to April 2024 at the end of the period, and fill in the missing months.

Image description
SQL Solution:

WITH Accounts AS (
    SELECT DISTINCT Name FROM trans
),
Months AS (
    SELECT DATEADD(MONTH, n, '2021-01-01') AS MonthStart
    FROM (
        SELECT TOP (DATEDIFF(MONTH, '2021-01-01', '2024-04-01') + 1) 
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1 AS n
        FROM master.dbo.spt_values
    ) AS Numbers
),
AccountMonths AS (
    SELECT a.Name, m.MonthStart
    FROM Accounts a
    CROSS JOIN Months m
),
Changes as (
    SELECT Name, DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0) as ym,
    sum(Credit - Debit) as change 
    FROM trans group by Name, DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0)
),
fullChanges as (
    SELECT A.Name,A.MonthStart,c.change 
    FROM AccountMonths a left join Changes c 
    on a.Name=c.Name and a.MonthStart=c.ym
)
SELECT 
   Name,YEAR(MonthStart) AS Y,MONTH(MonthStart) AS M,
   SUM(change) OVER (PARTITION BY Name ORDER BY MonthStart) AS balance
   FROM fullChanges

SQL does not have a convenient method to generate a months sequence, so nested queries and window functions are required, and the code is very complex.

SPL provides functions for generating dates sequence, including consecutive months.

Image description
A1: Query the database, group by account and date of the first day of each month, and calculate the monthly amount changes.

A2: Generate a continuous sequence consisting of the first day of each month. periods() generates a dates sequence, and @m represents the interval unit in month.

A3: Cross joint the account and date sequence.

A4: Left join the last cross-join result with A1.

A5: When the current account remains unchanged compared to the previous record, the current month's balance equals the change in the current month's amount plus the previous month's balance; When the account changes, the current month balance is reset to the current month amount change.

Open source address

Free download

...

🔧 Calculate monthly account balance and fill in missing dates:From SQL to SPL


📈 112.89 Punkte
🔧 Programmierung

🔧 Calculate Monthly Account Balance and Fill in Missing Dates — From SQL to SPL #10


📈 112.89 Punkte
🔧 Programmierung

🔧 Issue with spl-associated-token-account Version Conflict in anchor-spl v0.30.1


📈 42.61 Punkte
🔧 Programmierung

🔧 Calculate all the year/months included in the year/month interval — From SQL to SPL #15


📈 38.24 Punkte
🔧 Programmierung

🔧 Calculate a Pair of Minimum Values that Meet the Criteria within the Group — From SQL to SPL #12


📈 38.24 Punkte
🔧 Programmierung

🔧 Calculate the Hierarchy of Recursive References — From SQL to SPL #11


📈 38.24 Punkte
🔧 Programmierung

🔧 Calculate the hierarchy of recursive references :From SQL to SPL


📈 38.24 Punkte
🔧 Programmierung

📰 How to Check Amex Gift Card Balance at balance.amexgiftcard.com


📈 25.67 Punkte
📰 IT Security Nachrichten

📰 Should You Pay Your Credit Card Statement Balance or Current Balance?


📈 25.67 Punkte
📰 IT Security Nachrichten

🔧 AI Completes Human Photos! See How Reference Images Fill Missing Parts


📈 25.39 Punkte
🔧 Programmierung

🪟 Adobe Premier Pro gets AI, can fill in missing scene elements in video


📈 25.39 Punkte
🪟 Windows Tipps

📰 Data is the missing piece of the AI puzzle. Here's how to fill the gap


📈 25.39 Punkte
📰 IT Nachrichten

🔧 Get the Records after and before the Searched One — From SQL to SPL #18


📈 22.87 Punkte
🔧 Programmierung

🔧 Pair and Transpose Adjacent Records within the Group - From SQL to SPL #13


📈 22.87 Punkte
🔧 Programmierung

🔧 Get the records after and before the searched one:SQL VS SPL #12


📈 22.87 Punkte
🔧 Programmierung

🔧 Do ordered grouping and aggregation within groups:SQL VS SPL #11


📈 22.87 Punkte
🔧 Programmierung

🔧 Do Ordered Grouping and Aggregation within Groups — From SQL to SPL #1


📈 22.87 Punkte
🔧 Programmierung

🔧 Data Analysis Showdown: Comparing SQL, Python, and esProc SPL


📈 22.87 Punkte
🔧 Programmierung

🔧 How to prepare Trading and Profit & Loss Account and Balance Sheet in case of Incomplete Records?


📈 22.28 Punkte
🔧 Programmierung

🔧 Working with Dates and Times in SQL: Tips and Tricks


📈 21.74 Punkte
🔧 Programmierung

📰 How to switch from a Microsoft account to a local account, and tricks to avoid a Microsoft account


📈 21.53 Punkte
📰 IT Nachrichten

🔧 Find the superset from the relationship table:SQL VS SPL#13


📈 21.52 Punkte
🔧 Programmierung

🔧 Generate Calculated Columns Based on Continuous Values — From SQL to SPL #29


📈 21.52 Punkte
🔧 Programmierung

🔧 Add records that meet the criteria before each group after grouping :From SQL to SPL


📈 21.52 Punkte
🔧 Programmierung

🔧 How to execute SQL on CSV files with esProc SPL


📈 21.52 Punkte
🔧 Programmierung