SQL query with ORDER BY

I have a table in my SQL database that looks like this:

id    type      radius
-------------------------
1     type1     0.25
2     type2     0.59
3     type1     0.26
4     type1     0.78
5     type3     0.12
6     type2     0.45
7     type3     0.22
8     type3     0.98

I find it difficult to figure out how to define a query SELECTthat returns the lowest radius for each type.

In other words, the results I'm looking for will be as follows:

Results:

id    type      radius
-------------------------
1     type1     0.25
6     type2     0.45
7     type3     0.22

I suggest that I use ORDER BYto arrange the radius from the lowest to the highest level and capture the lowest. However, I also assume that I need to use DISTINCTfor a type, but I cannot figure out how to do this.

Does any SQL'ers expert have any ideas for this type of query SELECT? Any help is much appreciated!

Thanks a lot, Brett

0
source share
4 answers

,

SELECT type, MIN(radius) FROM table
GROUP BY type
ORDER BY type

, id min, , : . , ( )

SELECT t1.id,t1.type,t1.radius FROM table t1 
WHERE radius = ( 
     SELECT MIN(radius) FROM table
     WHERE radius = t1.radius
   )

( : , , Id, )

+6
+2

min GROUP BY

0

I believe this should work:

SELECT t1.id,t1.type,t1.radius FROM table GROUP BY type ORDER BY radius

As the default order increases, the minimum value should be shown.

0
source

Source: https://habr.com/ru/post/1784255/


All Articles