1

I have a query such as this:

SELECT
 cs1.id,
 cs1.uid,
 cs1.title,
 cs1.description
FROM related_chat_session rcs1
INNER JOIN chat_session cs1
ON cs1.id = rcs1.related_chat_session_id
WHERE
 rcs1.chat_session_id = 1ドル AND
 cs1.discoverable = true
ORDER BY random()
LIMIT 10

At the moment, it will return result in random order every time. However, I want to adjust the logic such that randomness would be stable based on a seed, where seed is (as an example) the day of the week.

i.e. The outcome would be that Monday results are listed in one order, Tuesday in another, etc.

How do I do this?

asked Jul 28, 2024 at 17:39
1
  • 1
    The goal is to test which related questions get highest CTR. However, I don't want to rotate them every time user refreshes the page because that's annoying user experience (you notice something, then try to do find it again, and it is no longer there). Commented Jul 29, 2024 at 8:06

1 Answer 1

3

Try this ORDER BY clause:

ORDER BY hashint8extended(
 cs1.id::bigint,
 extract(dow FROM current_timestamp)::bigint
 )

Hash functions return deterministic, bu seemingly random values.

answered Jul 29, 2024 at 7:03
1
  • This is perfect! Commented Jul 29, 2024 at 8:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.