79231647

Date: 2024-11-27 19:32:56
Score: 0.5
Natty:
Report link

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.

Fiddle

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 :

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: samhita