D
D
Daniil Sukhikh2021-04-17 22:11:56
SQL
Daniil Sukhikh, 2021-04-17 22:11:56

How to display two tops on different dates?

I have a query that displays the top users by the amount for 24 hours, how can I display it for the month as well? That it was in one request.

SELECT `id_user`, SUM(`sum`) `sum`, users.name FROM `balance_history` INNER JOIN `users` ON `id_user` = users.id WHERE balance_history.date >= NOW() - INTERVAL 1 DAY AND `type` = 'plus' GROUP BY `id_user` ORDER BY `sum` DESC LIMIT 5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-04-18
@rozhnev

For example like this:

SELECT 
  `id_user`, 
  SUM(IF(`balance_history`.`date` >= NOW() - INTERVAL 1 DAY, `sum`, 0)) `sum_24h`, 
  SUM(`sum`) `sum_1mnth`, 
  `users`.`name` 
FROM  `balance_history` 
INNER JOIN `users` ON `id_user` = users.id 
WHERE `balance_history`.`date` >= NOW() - INTERVAL 1 MONTH AND `type` = 'plus' 
GROUP BY `id_user` 
ORDER BY `sum` DESC 
LIMIT 5;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question