[ExactReplica]。[FilteredOpportunityProduct]表
Opportunityid baseamount
1 500
1 500
2 600
2 700
[ExactReplica] .FilteredOpportunity Table
Opportunityid name
1 ABC
2 CDF
我想取最大基数;但是,当存在基数的副本时我面临问题,我怎么才能只拿一个记录
我的查询
select
MaxAmount.[baseamount] ,
c.name
FROM [ExactReplica].FilteredOpportunity c
Left JOIN
(
SELECT opportunityid,
MAX((baseamount)) baseamount
FROM [ExactReplica].[FilteredOpportunityProduct]
GROUP BY opportunityid
) MaxAmount ON c.opportunityid = MaxAmount.opportunityid
inner JOIN
[ExactReplica].[FilteredOpportunityProduct] p ON MaxAmount.opportunityid = p.opportunityid
AND MaxAmount.baseamount = p.baseamount
答案 0 :(得分:1)
试试这个:
select max(baseamount) baseamount,a.name
from
(select
baseamount, ROW_NUMBER() over (partition by p.opportunityid,baseamount order by p.baseamount desc) rn,
c.name
FROM FilteredOpportunity c
inner JOIN
[FilteredOpportunityProduct] p ON c.opportunityid = p.opportunityid) a
where rn=1
group by a.name
<强>输出:强>
baseamount name
500 ABC
700 CDF
答案 1 :(得分:-1)
您可以尝试以下查询预期结果,我已执行以下脚本:
CREATE TABLE FILTEREDOPPORTUNITYPRODUCT( OPPORTUNITYID INT NULL, BASEAMOUNT VARCHAR(24)NULL )
CREATE TABLE FILTEREDOPPORTUNITY( OPPORTUNITYID INT NULL, NAME VARCHAR(24)NULL )
INSERT INTO FILTEREDOPPORTUN产品(OPPORTUNITYID,BASEAMOUNT)价值观 (1500),(1500),(2600),(2700)
插入FILTEREDOPPORTUNITY(OPPORTUNITYID,NAME)值 (1,&#39; ABC&#39),(2,&#39; CDF&#39)
选择
A.OPPORTUNITYID,B.NAME,MAX(BASEAMOUNT)作为BASEAMOUNT
来自FILTEREDOPPORTUNITYPRODUCT as a
加入FILTEREDOPPORTUNITY作为B
ON A.OPPORTUNITYID = B.OPPORTUNITYID
GROUP BY A.OPPORTUNITYID,B.NAME