Skip to content
Advertisement

Get all payment transactions recursively using SQL

I have a series of records. Based on the data below, how to get all transaction (XZ) for specific customer invoice (XR)?

Legend

Example 1

Condition

Desired output

Explanation above: There is Customer invoice (XR) with the amount of 36,247.62. There is payment recorded (XZ) with the value of 4.76 for document nos (9500011864). Since this is partial payment, there is a manual clearing balance (XA) with the amount of 36,242.86. Another Payment is posted (XZ) with the amount of 72,490.48 referring to document nos (9000001660).

Example 2

Condition

Desired output

Example 3

Condition

Desired output

How can I do this using SQL Server 2016? Is CTE Recursive applicable on this?

Advertisement

Answer

You need a recursive query. You select the XR record for the given “Doc Nos”. With its “Cleared Doc” you find related records (where “related” means they have this number in either “Doc Nos” or “Cleared Doc” depending on their TY). With all thus found records, you do the same with their “Cleared Doc”, etc. At last you show all XZ records from the found rows.

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