我在下面有一张表格,我需要查看给定时期内任何贷款编号的利率是否有所变化。下面只是示例数据,我需要在200万行中找到这些信息。如何才能做到这一点?
示例数据
+------------+-------------+---------------+
| DATE | LOAN_NUMBER | INTEREST_RATE |
+------------+-------------+---------------+
| 04/01/2010 | 1111122 | 0.375 |
| 05/15/2011 | 1111122 | 0.375 |
| 06/01/2012 | 1111122 | 0.300 |
| 07/08/2010 | 1111133 | 0.400 |
| 07/01/2011 | 1111133 | 0.450 |
+------------+-------------+---------------+
我尝试执行以下操作:
Select A.Date, A.Loan_Number, A.Interest_Rate
FROM TABLEX A
INNER JOIN TABLEX B ON A.Loan_Number = B.Loan_Number
AND A.Date < B.Date
但这并没有让我得到我需要的结果。
任何帮助表示赞赏!
答案 0 :(得分:0)
Select A.Date, A.Loan_Number, A.Interest_Rate
, B.Date, B.Interest_Rate
FROM TABLEX A
INNER JOIN TABLEX B
ON A.Loan_Number = B.Loan_Number
AND DateDiff(dd, A.Date, B.Date) =< 10
AND DateDiff(dd, A.Date, B.Date) > 0
AND A.INTEREST_RATE <> B.INTEREST_RATE
答案 1 :(得分:0)
你可以使用row_number来模仿下一个函数,比如
select b.*
from (
Select [Date], Loan_Number, Interest_Rate,row_number() over(partition by Loan_Number order by [Date] desc, Interest_Rate desc) rowid
FROM TABLEX) A
join (
Select [Date], Loan_Number, Interest_Rate,row_number() over( partition by Loan_Number order by [Date] desc, Interest_Rate desc) rowid
FROM TABLEX) B
on a.rowid = b.rowid -1
and a.Loan_Number = b.Loan_Number
where a.Interest_Rate <> b.Interest_Rate
答案 2 :(得分:0)
Interest_Rate
已更改意味着MIN
值与MAX
值不同。
该查询将返回Loan_Numbers
在给定时间段内(从Interest_Rate
包含到@StartDate
独占)已更改的所有@EndDate
的列表。
SELECT A.Loan_Number
FROM TABLEX A
WHERE
A.Date >= @StartDate
AND A.Date < @EndDate
GROUP BY A.Loan_Number
HAVING MIN(A.Interest_Rate) <> MAX(A.Interest_Rate)
;