A
A
AlexeyID2018-08-22 12:06:39
SQL
AlexeyID, 2018-08-22 12:06:39

How to sort and select only popular posts?

Tell me how to implement the selection and sorting of only popular records
Table:
id|timestamp|rating
How to implement the following:
Sort for the beginning by time, then re-sort what happened after the first sorting but by rating (during the day) and so that in the final selection for one day there were no more than 10 records

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Kupriyanov, 2018-08-23
@AlexeyID

SELECT
  `id`,
    `timestamp`,
    `rating`
FROM
     (
    SELECT
      `id`,
      `timestamp`,
            `rating`,
      @timestamp_rank := IF(@current_timestamp = `timestamp`, @timestamp_rank + 1, 1) AS `timestamp_rank`,
      @current_timestamp := `timestamp`
    FROM
      `rating_log`
        ORDER BY
      `timestamp` DESC,
            `rating` DESC
     ) `ranked`
WHERE
  `timestamp_rank` <= 10;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question