Use ZRANGEBYSCORE to get the 5 highest scores - Redis - redis

Looking to retrieve the five highest scores from a ZSET in redis. Checking ZRANGEBYSCORE, it looks like I can use +inf to retrieve the highest score, but not sure if there is a easy way to retrieve the next four highest scores. I don't know any of the scores.
My purposed solution would be something similar in SQL to ORDER BY score DESC LIMIT 5

The Redis equivalent should be:
ZREVRANGEBYSCORE myzset 0 4

Related

Is it possible to assign a `rank` from another field using a groq query

I only have a score and entrant field, but want to assign a rank based on the score value. Where scores are equal, the rank is equal, but then would skip over the next n ranks to "realign" the rank.
rank
score
entrant
1
100
"Bob"
1
100
"Jon"
3
90
"Jen"
4
80
"Jim"
At the moment I am adding the rank field with JavaScript after I get the results, but want to know if it's possible to do this with the groq query?

Retrieve from the database rows ordered by a boolean column, but also random as a second criterion

I have an Items table:
id | name | is_featured
1 name1 false
The items from this table I need to show them in a random way, but is_featured first. The rules are:
If count is_featured=True > 6, get is_featured=True all, and randomize and get first 6
If count is_featured=True < 6, get is_featured=True all, and randomize
Count how many are missing up to 6. Get from non-featured random the remaining items.Unite lists.
I do this in 2-3 steps in database:
first count the number of featured
first get featured then get the rest if is needed, and randomize(in backend)
It is possible to do that in one step, in database?
You seem to want six rows, with the is_featured first. You can do this in one step:
select i.*
from items i
order by i.is_featured desc, -- true is first
random()
fetch first 6 rows only;
That is, sort all the data with is_featured first. Then choose the first six.

Filtering elements in Redis

I am new to Redis, so excuse the question.
I am trying to make a list of hashes. For example (in JSON):
{
userList: [
{ id: 1, name: 'Foo', ranking: 10 },
{ id: 2, name: 'Bar', ranking: 5 }
]
}
And then I want to:
Retrieve all the hashes (users) that have a ranking less than 10.
Delete all the hashes who have ranking 0.
How do you implement the last schema in Redis? Is it possible?
How do you filter the elements, and remove some of them?
How do you implement the last schema in Redis? Is it possible?
Redis is schema-less. Let's call what you need a data storage approach.
One possible approach is using HSET or HMSET to add these JSON objects by id, where their id is the key and the JSON text is the value. We'll call this hash as users:byid.
This is the first part of the problem. Now you can get objects by id.
Now the next issue is you want to retrieve objects in a range of what you call ranking. In order to get this, you need to store your objects in a sorted set using ZADD. Sorted sets are sorted by score, and items are stored with a score. It sounds perfect for your use case!
Actually you're going to store the object ids in the whole sorted set:
zadd users:byranking 10 1 5 2
... where 10 is the score (i.e. your actual ranking value) and 1 the id and so on.
So, how you filter items by ranking? Using ZRANGEBYSCORE:
By ranking between 0 and 10, excluding 10. zrangebyscore users:byranking 0 (10
By ranking between 0 and 10, including 10. zrangebyscore users:byranking 0 10
The so-called ZRANGEBYSCORE will give you the ids of retrieved users. How you get their JSON text? Using HMGET:
HMGET users:byid 1 2
...which will get both user with id 1 and 2, if 10 ranking is inclusive.

Retrieving ID's order by date with pagination where between two dates

I have sorted set like this:
Student Registrations
ID SCORE
1 1437809702647
2 1437770121955
3 1437766535972
I want to retrieve student ids between two dates and ordered in 2nd page and 3rd page (every page has 10 records)
what would be your proposed?
I found my solution:
zrangebyscore student:registrations 1431550800000 1432069200000 LIMIT 0 10 WITHSCORES
Detailed:
zrangebyscore / zrevrangebyscore
KEY
WHERE_CONDITION(min max)
PAGINATION(LIMIT page*rowCountPerPage rowCountPerPage)
for sorting zrangebyscore or zrevrangebyscore

postgreSQL get index of a row that is outside the limit you searched for

I am new to SQL and I am not sure how to properly search my question so I will ask here.
Please see this link to see the SQL tables and queries I am working with
In this example there are 6 rows and I am limiting my search to start at the first index and give me at most 2. However, I would like to know the index of the row that has id 1.
When I use the query I describe in sqlfiddle, It shows me rows with id 5 and 23. But it doesn't include the row with id 1. However, I need to know the index of the row with id 1..
click here to see the full list
The link above prints out all of the rows and we can see that index 3 has the row containing id 1.
However I need to know that index without asking for the entire Array.
Why is this important? Well, lets say that we have 1 million rows. And if I ask for a million rows, that would mean allocating an array of one million. I could parse the array until I find the id I am looking for. However, allocating a million is way too costly.
Lets say for example that the row I am looking for resides in index 26, But I make my query so that it starts at index 0 and limits to 10. The array that I get from this query would not contain index 26. However I still need to know that it IS at index 26.
So this magic query would give me two things:
the top ten rows of the sorted rows
the index of a specified id (e.g. id of 1) regardless of its placement in the list.
Is this a possible query?
Clarification:
I use the word index to mean the row number.
If a we query a list of names from the db, we could get something like this:
bob
frank
dawn
then bob would be at index 0, frank would be at index 1 and dawn at index 2.
If I ORDER BY name ASC then the list of names would become
bob
dawn
frank
bob would be index 0 dawn would be index 1 and frank would be index 2.
I hope this makes things more clear.
If you want the row number, use the row_number() function:
SELECT *
FROM (SELECT ud.id, ud.team_name, ui.name, ui.date_created,
row_number() over (order by ui.name, ui.id) as rownumber
FROM user_data ud JOIN
user_infos ui
ON ui.id = ud.id
WHERE ui.date_created BETWEEN NOW() - INTERVAL '1 year' AND NOW()
) t
WHERE rownumber <= 10 or id = 1;
If you want them in order, just add order by rownumber as the last statement.