我有一个看起来像这样的表:
我要执行的操作是选择MBAPaystring_48months(如果它不为NULL),否则选择MBAPaystring_12months,并在该薪水字符串的左侧附加一定数量的“ X”。 X的数量将通过采用一个声明的日期并减去该日期和InsertedDate的月份数来计算。
我不确定到目前为止如何实现这一结果。
DECLARE @archivedate date = '5/1/2019';
WITH paystring AS(
SELECT DISTINCT Ph.LoanNumber, Ph.MBAPaystring_48months, MBAPaystring_12months, InsertedDate
FROM MARS_DW..ValuationPaymentHistory_Archive PH
INNER JOIN(
SELECT LoanNumber, MAX(InsertedDate) as [Most Recent Inserted Date]
FROM MARS_DW..ValuationPaymentHistory_Archive
GROUP BY LoanNumber
) as id on id.LoanNumber = PH.LoanNumber
)
SELECT
CASE WHEN paystring.MBAPaystring_48months IS NOT NULL THEN paystring.MBAPaystring_48months ELSE paystring.MBAPaystring_12months
END AS [the pay string of strings]
FROM paystring
SELECT MBAPaystring_12months, DATEDIFF(month, InsertedDate, @archivedate) [num]
FROM paystring