我在类似的问题上没有找到答案。如何在子查询上使用聚合函数,如
当我在我的数据库中查询名为ratioPnLTable的表时,如下所示:
SELECT DISTINCT strat_id, ratio, root FROM ratioPnltable
WHERE totalPnl IN (SELECT DISTINCT MAX(TotalPnl) FROM ratioPnlTable
GROUP BY strat_id)
我得到了这个结果:
strat_id ratio root
1 2.6 AD
1 2.7 AD
1 2.8 AD
2 1.4 ED
2 1.5 ED
2 1.6 ED
3 1.9 HG
3 2.0 HG
3 2.1 HG
相反,我希望只有这样的比率的最小值:
strat_id ratio root
1 2.6 AD
2 1.4 ED
3 1.9 HG
答案 0 :(得分:1)
根据您的示例结果,您可以使用row_number()
执行所需操作,而根本不需要聚合:
select t.*
from (select t.*,
row_number() over (partition by strat_id order by ratio) as seqnum
from ratioPnltable t
) t
where seqnum = 1;
答案 1 :(得分:1)
这不应该只是一个MIN()语句吗?除非Root可以不同......
SELECT
strat_id,
MIN(ratio) ratio,
root
FROM
ratioPnltable
WHERE
totalPnl IN (query)
GROUP BY
strat_id,
root