My SQL Server database includes all contracts. my query finds the contract value for the latest date, I need another column that shows the contract value for the second last contract.
Example:
ContractID ordd_mn_end_date ord_mn_billed_amt
-------------------------------------------------
8198 10-31-2021 2574.43
8198 10-31-2020 833.15
What I would like to happen is for the ExpiringAmt 2 to show 833.15
Code
SELECT
det.ordd_ContractItemID,
det.ordd_mn_end_date,
det.ordd_mn_billed_amt AS Expiring_Amt,
det.ordd_mn_billed_amt AS Expiring_Amt2
FROM
ccmast_restore_20201021.dbo.lti_orddet det
WHERE
det.ordd_mn_end_date IN (SELECT MAX(det.ordd_mn_end_date)
FROM ccmast_restore_20201021.dbo.lti_orddet det
GROUP BY det.ordd_ContractItemID)
AND det.ordd_ContractItemID IN (8198)
ORDER BY
det.ordd_mn_end_date DESC