Can anyone tell me why this throws an error?
WITH date_diffs AS
(
SELECT
ctd_item_id,
ctd_assigned_to,
DATEDIFF(d, ctd_assigned_date, ctd_review_completed_date) AS DATE_DIFF,
ROW_NUMBER() OVER (PARTITION BY ctd_item_id ORDER BY date_diff DESC) AS RN
FROM
irt_item_tracking_detail_tbl
)
SELECT
*
FROM
date_diffs
WHERE
date_diffs.rn = 1
Error is
Invalid column name 'date_diff'
Can it not be created within the SELECT
statement in the WITH
?