0

The following query gives me a single row because b.id is pinned. I would like a query which I can give a group of ids and get the minimum valued row for each of them.

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.


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.

1 Answer 1

1

SQL Server ranking function should do the trick.

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's exactly what I was looking for. One note, the columns need to be renamed or there will be errors like column 'id' specified multiple times for Z.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.