Select 具有不同最大值(列值)的行
Select row with Distinct max(column value)
[ExactReplica].[FilteredOpportunityProduct] Table
Opportunityid baseamount
1 500
1 500
2 600
2 700
[ExactReplica].FilteredOpportunity Table
Opportunityid name
1 ABC
2 CDF
我要取最大基数;但是,当存在重复的 baseamount 时,我会遇到问题,我怎样才能只记录一条记录
我的查询
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
试试这个:
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
您可以尝试以下查询以获得预期结果吗,我已经执行了以下脚本:
对于Table创作:
创建 TABLE FILTEREDOPPORTUNITYPRODUCT (
机会 ID INT NULL,
BASEAMOUNT VARCHAR(24) 空值
)
创建 TABLE 过滤机会(
机会 ID INT NULL,
名称 VARCHAR(24) NULL
)
插入:
插入 FILTEREDOPPORTUNITYPRODUCT (OPPORTUNITYID,BASEAMOUNT) 值
(1,500),(1,500),(2,600),(2,700)
插入已过滤的机会(机会 ID,名称)值
(1,'ABC'),(2,'CDF')
选择:
SELECT
A.OPPORTUNITYID,B.NAME,MAX(BASEAMOUNT) 作为 BASEAMOUNT
FROM FILTEREDOPPORTUNITYPRODUCT AS
以 B
身份加入筛选机会
ON A.OPPORTUNITYID = B.OPPORTUNITYID
按 A.OPPORTUNITYID、B.NAME
分组
[ExactReplica].[FilteredOpportunityProduct] Table
Opportunityid baseamount
1 500
1 500
2 600
2 700
[ExactReplica].FilteredOpportunity Table
Opportunityid name
1 ABC
2 CDF
我要取最大基数;但是,当存在重复的 baseamount 时,我会遇到问题,我怎样才能只记录一条记录
我的查询
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
试试这个:
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
您可以尝试以下查询以获得预期结果吗,我已经执行了以下脚本:
对于Table创作:
创建 TABLE FILTEREDOPPORTUNITYPRODUCT ( 机会 ID INT NULL, BASEAMOUNT VARCHAR(24) 空值 )
创建 TABLE 过滤机会( 机会 ID INT NULL, 名称 VARCHAR(24) NULL )
插入:
插入 FILTEREDOPPORTUNITYPRODUCT (OPPORTUNITYID,BASEAMOUNT) 值 (1,500),(1,500),(2,600),(2,700)
插入已过滤的机会(机会 ID,名称)值 (1,'ABC'),(2,'CDF')
选择:
SELECT
A.OPPORTUNITYID,B.NAME,MAX(BASEAMOUNT) 作为 BASEAMOUNT
FROM FILTEREDOPPORTUNITYPRODUCT AS
以 B
身份加入筛选机会
ON A.OPPORTUNITYID = B.OPPORTUNITYID
按 A.OPPORTUNITYID、B.NAME