AndyHamp
AndyHamp

Reputation: 37

SQL return lowest value for each unique integer value

I have data similar to this:

    loc  dist  value
    1    0.2    2.5
    1    0.5    3.8
    1    0.8    3.4
    1    1.2    5.9
    1    1.4    5.6
    1    1.8    6.2
    1    2.1    7.8
    1    2.4    7.4 

What I need to be able to read the lowest value for each dist truncated (rounded was a red herring) to a integer. The data is in loc, dist sequence ... the value col is not in sequence

ie the result should give me:

loc  dist  value
1    0     2.5
1    1     5.6
1    2     7.4

Obviously I can do this in code but i was trying to do this purely in SQL.

Any suggestions ?

Upvotes: 1

Views: 48

Answers (2)

Super Mario
Super Mario

Reputation: 939

Do you want round or floor?

select loc, round(dist) as dist, min(value) as value
from my_table 
group by loc, round(dist)
order by dist asc

Upvotes: 3

The Impaler
The Impaler

Reputation: 48770

How about:

select floor(dist), min(value) from my_table group by floor(dist)

Upvotes: 3

Related Questions