sql - How to select ten values, five larger and five smaller than a given value in one Postgre query? -
I have an interesting question to ask for you :) Beliefs: I'm using Spring and Hibernate on the Postgraduate Database .
I have a user table and every user has his name and his score is one way I want to generate a rank where: Those who have more points than the given users and 5 users, such as low scores:
- user1 p
- user2 p.104
- user3 P.103
- user4 p.102
- given user p.100
- user6 p.99
- user7 p.98 < / Li>
- ...
My question is: Is it possible to make a question in it? Will it be more optimal and faster in a query? Do you have any proposal for such a question? It is very easy to do it in two questions but I want to know what is the way to do this at a time?
PostgreSQL can do this with a single query. Here's a way you might need to use native (raw) SQL in Hibernate.
at least (select * from the exam where the score is & lt; (choose score from the exam where user_id = 13) range 5), as the higer (*; (exam Choose the score from where user_id = 13) range 5) Choose * at least from the union * Select all the selections from the 'Higgs union', where user_id = 13 order from the order; What you should do, should think about that if there are five points more or five points lower than the not target (user_id = 13). Also, this query will not fetch in any rows, which are similar to the target score, which you may be looking for or not. This 10 rows that do not attempt to achieve the closest goal.
On my Dev box, which is not speed, it runs in 0.1 ms in the query table. One million lines
Comments
Post a Comment