gpt4 book ai didi

sql - 合并两个 View

转载 作者:行者123 更新时间:2023-12-04 05:31:04 24 4
gpt4 key购买 nike

我有两个 View A 和 B 都具有相同的列名称 StudentID, FeeToBePaid, AmountPaid .在两个 View 中 StudentID是一样的。但栏目FeeToBePaidAmountPaid是不同的。

我想用 A 中的行替换 B 中的行 if A.FeeToBePaid <= A.AmountPaidstudentID .

View A

SchoolStudentID     sFeeToBePaid     amountpaid
12-S-3003 1000.00 1000.00

View B
SchoolStudentID   sFeeToBePaid   amountpaid
12-S-3003 1000.00 0.00
11-F-2019 NULL 1000.00
12-S-3009 NULL 1000.00

我需要答案
SchoolStudentID sFeeToBePaid amountpaid
  12-S-3003         1000.00        1000.00
11-F-2019 NULL 1000.00
12-S-3009 NULL 1000.00

最佳答案

结合两个LEFT JOIN的结果试试吧使用 UNION没有 ALL 来消除重复记录。

SELECT a.SchoolStudentID , 
CASE WHEN a.sFeeToBePaid <= a.amountpaid
THEN a.sFeeToBePaid
ELSE b. sFeeToBePaid
END AS sFeeToBePaid,
CASE WHEN a.sFeeToBePaid <= a.amountpaid
THEN a.amountpaid
ELSE b.amountpaid
END AS amountpaid
FROM ViewA a
LEFT JOIN ViewB b
ON a.SchoolStudentID = b.SchoolStudentID
UNION
SELECT a.SchoolStudentID ,
CASE WHEN b.sFeeToBePaid <= b.amountpaid
THEN b.sFeeToBePaid
ELSE a. sFeeToBePaid
END AS sFeeToBePaid,
CASE WHEN b.sFeeToBePaid <= b.amountpaid
THEN b.amountpaid
ELSE a.amountpaid
END AS amountpaid
FROM ViewB a
LEFT JOIN ViewA b
ON a.SchoolStudentID = b.SchoolStudentID

SQLFiddle Demo

关于sql - 合并两个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12616345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com