Select only 24 records per day

Asked

Viewed 87 times

4

I need help to elaborate a query that selects only 24 records per day.

For example:

Day 1: 100 records
Day 2: 50 records
Day 3: 34 records

I want to take only 24 of each day: 1, 2 and 3...(in this example I would add: 72 records)

SELECT date_format(hora,'%d/%m/%Y'), SUM(ponto) FROM `pontos` WHERE user = "teste" AND hora >= '2019-08-20' AND hora < NOW() 

*As it stands, my query brings the sum of all records

inserir a descrição da imagem aqui

What’s the best way to do that?

  • 1

    Charles, you want to pick up only 1 value an hour, is that it? type, take the time value 00:00 of the day, then the time value 01:00 of the day and so until give the 24 hours and add up, this is it?

  • How do you know that the value 170 is not the result of the 24 records? What is in the column "point"?

  • has the value 1 (ONE), where I want to catch 24 points each day

  • in one day the guy can do as many points as he wants (more than MIL), but I want to catch only 24 per day

  • @Heathcliff yes, this is a solution that solves too, because picking 1 point per hour I will have the 24 of each day.

  • @Sam believes that the NOW command considers the time of the record, where today’s records (as per print) entered at a time shorter than the NOW time

  • I posted an updated reply now. See if it solves.

Show 2 more comments

2 answers

4


Use a subquery with LIMIT 24. Create an alias for date_format(hora,'%d/%m/%Y') and use GROUP BY in that alias to return all dates with the result of SUM(ponto):

SELECT date_format(hora,'%d/%m/%Y') AS dia, SUM(ponto) FROM
(SELECT hora, ponto
      FROM `pontos`
      WHERE user = "teste" AND hora >= '2019-08-20' AND hora < NOW()
      LIMIT 24
) AS subquery
GROUP BY dia

Check on the SQLFIDDLE

  • thank you very much! I believe that this is the right way...however the query brought only the 24 points of day 2, I would like to bring the sum of the previous days from the date 2019-08-20, in this image da para ver os registros das datas anteriores: https://uploaddeimagens.com.br/imagens/table-png-e590c062-3755-4bc8-93aa-3545aa8a8858

  • I updated the code on the answer. Try it again.

  • It hasn’t worked here yet, https://uploaddeimagens.com.br/images/002/313/781/full/query.png?1567441302, but I saw your test on SQLFIDDLE and that’s exactly what I need, I’ll try to find out here why I didn’t bring the same result.

  • Try to see which version of your Mysql.

  • Opa, I removed the LIMIT 24 and now it brought the results of the sum of the day: https://uploaddeimagens.com.br/imagens/query-png-0c3a1667-0935-4f7a-b364-fd49228888b8, with this I will do an if in php where if the value is greater than 24, it considers only 24 and sums up everything.

  • 1

    Quite strange this, the limit should work :(... But if the answer was favorable, I believe it deserves to end the question with .. Abs!

  • Thank you so much for your help! Abs!

Show 2 more comments

1

You can use the LIMIT:

SELECT date_format(hora,'%d/%m/%Y'), SUM(ponto)
FROM pontos
WHERE user = "teste"
  AND hora >= '2019-08-20'
  AND hora < NOW()
LIMIT 24
  • did not work...because even with LIMIT 24, the query continues to bring the sum of the total records of the day. *I attached an image in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.