E
E
Evgeny_A2020-06-16 21:41:44
SQL
Evgeny_A, 2020-06-16 21:41:44

How to sort by three fields as expected?

Greetings.

The table has three fields:

  1. score-int
  2. distance-int
  3. peoples-int

I need to display a record that has:
  1. Highest score
  2. smallest distance
  3. largest peoples

Concocted a request:

SELECT * FROM Table ORDER BY score DESC, distance ASC, peoples DESC

I expect that the result will be entries at the beginning that best fit all the criteria at once. But in fact, the priority, which depends on the order of the fields, affects so much that the bias in one parameter is too large. It would be desirable something like "natural sorting" on three fields.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lazy @BojackHorseman, 2020-06-16
@Evgeny_A

formulate a criterion that matches your expectations and sort by its value

D
Dmitry Ivanov, 2020-06-16
@Dmitronid

I don't know, of course, but maybe it will work?

SELECT * FROM Table where max(score) and min(distance) and max(peoples);

D
d-stream, 2020-06-16
@d-stream

You can try sorting in excel)
In general, order by with a list of sorting criteria sounds exactly like sorting first by the first criterion inside the same - by the second and so on ...
And what you want - it suggests itself as the first criterion - something synthetic from all three criteria. For example, if these metrics are represented as coordinate axes, then the distance from the origin can be used as a synthetic metric - that is, the square root of the sum of squares (in this case, the root can be excluded for ordering).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question