我正在尝试以一种方式过滤查询,以使结果仅显示具有重复值的帐户。就目前而言,我的代码给了我单一和重复的帐户。
这是我当前的查询:
SELECT
l.AcctNum,
l.SymID,
l.PaymentHistory_1,
h.postdatetime,
h.NewCHAR
FROM Loans AS L WITH (nolock)
LEFT JOIN History AS H WITH (nolock) ON l.acctnum = h.acctnum
WHERE l.paymenthistory_1 = '12'
AND l.SymID LIKE '0%'
AND h.NewCHAR LIKE 'Rate decreased from%'
ORDER BY l.acctnum
目前,我会看到如下结果:
AcctNum Sym ID PaymentHistory PostDate New CHAR
123 1 blah 1/1/2000 Rate decreased from
125 1 blah 1/1/2000 Rate decreased from
160 1 blah 1/1/2000 Rate decreased from
160 1 blah 1/1/2000 Rate decreased from
我想看的是这个
AcctNum Sym ID PaymentHistory PostDate New CHAR
160 1 blah 1/1/2000 Rate decreased from 2 to 1
160 1 blah 1/1/2000 Rate decreased from 3 to 2
答案 0 :(得分:0)
您可以尝试:
SELECT AcctNum
, SymID
, PaymentHistory_1
, postdatetime
, NewCHAR
FROM (
SELECT l.AcctNum
, l.SymID
, l.PaymentHistory_1
, h.postdatetime
, h.NewCHAR
, COUNT(1) OVER(PARTITION BY l.AcctNum) AS CountAcctNum
FROM Loans AS L WITH(nolock)
LEFT JOIN History AS H WITH(nolock) ON l.acctnum = h.acctnum
WHERE l.paymenthistory_1 = '12'
AND l.SymID LIKE '0%'
AND h.NewCHAR LIKE 'Rate decreased from%'
) LoansHistory
WHERE CountAcctNum > 1
ORDER BY AcctNum