root@debianlpi:~# cat /etc/apt/apt.conf
#Acquire::http::Proxy "http://idcproxy.compabc.com:80";
我的目标是使用Select * from LoanAccount main INNER JOIN LoanSubAccount sub
WHERE main.LoanAccountID = sub.LoanAccountID
AND sub.LoanStatus = 4
检索行,但使用LoanStatus = 4
的记录替换金额。
最终结果预计为
答案 0 :(得分:4)
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY LoanAccountID, LoanStatus
ORDER BY LoanSubAccountID) rn
FROM LoanSubAccount
)
SELECT t1.LoanSubAccountID,
t1.LoanAccountID,
t1.LoanStatus,
t1.CommodityType,
t2.Amount
FROM cte t1
INNER JOIN cte t2
ON t1.rn = t2.rn AND
t1.LoanStatus > t2.LoanStatus
我宁愿展示一张代表上述CTE外观的表格,而不是给出详细的解释:
rn | LoanSubAccountID | LoanAccountID | LoanStatus | CommodityType | Amount
1 | 1 | 1 | 2 | 1 | 100
2 | 2 | 1 | 2 | 2 | 200
1 | 3 | 1 | 4 | 3 | 150
2 | 4 | 1 | 4 | 4 | 150
如果我正确阅读了您的要求,您希望从两种不同的贷款状态中连接具有相同行号的行。我上面给出的连接查询就是这样做的。