I'm doing a query whose purpose is to put a label called Match
and then filter that Match with WHERE
, but it generates an error "Column name does not exist".
My script is as follows:
SELECT
[Contra], [Schedule],
SUM(CAST([UsedSM] AS float)) AS [UsedSM], SUM(CAST([TotalSU] AS int)) AS [TotalSU],
SUM(CAST([UsedSU] AS float)) AS [UsedSU],
MatchExpireDate = Case When FIRST_VALUE(Schedule) OVER (PARTITION BY [Contra]
ORDER BY [Schedule] ASC
ROWS UNBOUNDED PRECEDING
) = [Schedule] Then 'Match'
Else 'Not' End
FROM [dbMartxxx].[dbo].[tblPDPcontracts] where MatchExpireDate = 'Match'
GROUP BY [Contra], [Schedule]
ORDER BY [Contra] DESC;
GO