We dont have the example data for tables like cs_facilities, cs_fundingMethods, ci_periodicBillings etc.
I created this example based on the sample data you provided and the expectation that you shared.
WITH RateChanges AS (
SELECT
Client,
Rate,
Funding,
LastUpdated,
LAG(Rate) OVER (PARTITION BY Client ORDER BY LastUpdated DESC) AS PreviousRate,
LAG(Funding) OVER (PARTITION BY Client ORDER BY LastUpdated DESC) AS PreviousFunding
FROM ClientBillingInfo
WHERE LastUpdated BETWEEN '2024-09-01' AND '2024-09-30' -- Filtering only September 2024
)
SELECT
Client,
Rate AS LatestRate,
PreviousRate,
Funding AS LatestFunding,
PreviousFunding,
LastUpdated
FROM RateChanges
WHERE PreviousRate IS NOT NULL -- Ensures that there's a previous rate (indicating a change)
ORDER BY Client, LastUpdated DESC;
Output :