我试图获得以下条件: 每个LID与多个BID相关联,并且每个BID具有AvgTrustValue。 我希望找到每个LID具有最低AvgTrustValue的BID
我做了以下事情:
SELECT DISTINCT LID, T1.BID, AvgTrustValue
FROM Loans T1, Repayments T2,
(SELECT BID, AVG(trust_value) As AvgTrustValue
FROM Assign
GROUP BY BID) T3
WHERE T1.LoanID = T2.LoanID
AND T1.BID = T3.BID
ORDER BY LID
但它只给我以下内容:
| LID | BID |
|S2262450A | S7444319C | 82
|S2848191X | S7611209X | 76
|S2848191X | S9981233W | 100
|S4495282I | S7444319C | 82
|S4792394D | S7444319C | 82
|S4792394D | S7611209X | 76
|S7145303Q | S7611209X | 76
|S7173102K | S7444319C | 82
我希望得到以下内容:
| LID | BID |
|S2262450A | S7444319C | 82
|S2848191X | S7611209X | 76
|S4495282I | S7444319C | 82
|S4792394D | S7611209X | 76
|S7145303Q | S7611209X | 76
|S7173102K | S7444319C | 82
我也试过以下但失败了:
SELECT DISTINCT LID, T1.BID, min(AvgTrustValue)
FROM Loans T1, Repayments T2,
(SELECT BID, AVG(trust_value) As AvgTrustValue
FROM Assign
GROUP BY BID) T3
WHERE T1.LoanID = T2.LoanID
AND T1.BID = T3.BID
GROUP BY LID
ORDER BY LID
非常感谢你。
答案 0 :(得分:2)
您可以使用窗口函数row_number
为每个盖子获取一行,且AvgTrustValue最少:
select *
from (
select t.*, row_number() over (
partition by lid order by AvgTrustValue
) as rn
from your_table t
) t
where rn = 1;
或使用TOP with ties
:
select top 1 with ties *
from your_table
order by row_number() over (
partition by lid order by AvgTrustValue
);
答案 1 :(得分:0)
我认为'不存在'也没关系
从your_table a中选择lid,bid,avgvalue 哪里不存在(从your_table中选择1,其中lid = a.lid和avgvalue> a.avgvalue)