简体   繁体   English

SQL:基于多个最小值组选择多个列?

[英]SQL: select multiple columns based on multiple groups of minimum values?

The following query gives me a single row because b.id is pinned. 以下查询给了我一行,因为b.id已固定。 I would like a query which I can give a group of ids and get the minimum valued row for each of them. 我想要一个查询,我可以给它一组id并为每个id获得最小值的行。

The effect I want is as if I wrapped this query in a loop over a collection of ids and executed the query with each id as b.id = value but that will be (tens of?) thousands of queries. 我想要的效果就像我将这个查询包装在一个id集合中的循环中,并以每个id为b.id = value执行了查询,但这将是(数十个)数千个查询。


select top 1 a.id, b.id, a.time_point, b.time_point
from orientation_momentum a, orientation_momentum b
where a.id = '00820001001' and b.id = '00825001001'
order by calculatedValue() asc

This is on sql-server but I would prefer a portable solution if it's possible. 这是在sql-server上,但如果可能的话,我希望使用便携式解决方案。

SQL Server ranking function should do the trick. SQL Server排名功能应该可以解决问题。

select * from (
select a.id, b.id, a.time_point, b.time_point, 
rank() over (partition by a.id, b.id
order by calculatedValue() asc) ranker
from orientation_momentum a, orientation_momentum b
where a.id = '00820001001' and b.id between 10 and 20
) Z where ranker = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM