I have this query
x
SELECT
L.Account,
L.PaidOffDate
FROM
MARS.dbo.vw_Loans L
WHERE
L.isActive = 'False'
AND L.LoanStatus NOT LIKE '%REO%'
AND L.LoanStatus <> 'Trailing Claims'
)
UNION
-- loan numbers for REO sold
(
SELECT
L.Account,
R.SoldDate
FROM
MARS.dbo.vw_REO R
LEFT JOIN
MARS.dbo.vw_Loans L ON L.ConvertedToPropertyID = R.PropertyID
WHERE
R.Active = 0
AND R.Status = 'Sold'
AND L.isActive = 'False'
AND L.LoanStatus LIKE '%REO%'
)
I put this as a common table expression, how do I select R.SoldDate in that case?
Specifically I am trying to run this
CASE
WHEN Inactives.PaidOffDate IS NOT NULL
AND Inactives.PaidOffDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, @reportingDate), -1) AND @reportingDate
THEN Inactives.PaidOffDate
ELSE
CASE
WHEN Inactives.SoldDate IS NOT NULL
AND Inactives.SoldDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, @reportingDate), -1) AND @reportingDate
THEN Inactives.SoldDate
ELSE ''
END
END AS [CHECKING PIF DATE]
But I cannot select Inactives.SoldDate
Advertisement
Answer
I think what you are after here is a computed column, something like this:
SELECT
L.Account,
L.PaidOffDate,
'Payoff Date' AS Type
FROM MARS.dbo.vw_Loans L
WHERE
L.isActive = 'False' AND
L.LoanStatus NOT LIKE '%REO%' AND
L.LoanStatus <> 'Trailing Claims'
UNION ALL
SELECT
L.Account,
R.SoldDate,
'Sale Date'
FROM MARS.dbo.vw_REO R
LEFT JOIN MARS.dbo.vw_Loans L
ON L.ConvertedToPropertyID = R.PropertyID
WHERE
R.Active = 0 AND
R.Status = 'Sold' AND
L.isActive = 'False' AND
L.LoanStatus LIKE '%REO%';
The computed column Type
keeps track of the origin of each record, so that you will know this after the union has been peformed. Note that I switched to UNION ALL
, assuming that you always wanted to retain all records.