-5

i want to count the data in current date last hour (for example now() is 2020-04-11 12:30:00 , then i want to get data before 12. any idea ?

table_a

timestamp           
2020-04-11 10:00:00     
2020-04-11 10:00:00     
2020-04-11 11:00:00     
2020-04-11 12:00:00  
2020-04-11 12:30:00 

expected results:

timestamp            count
2020-04-11 10:00:00     2
2020-04-11 11:00:00     1
2020-04-11 12:00:00     1

i'm using mysql 8

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
hinafaya
  • 111
  • 1
  • 4
  • 16

2 Answers2

1

Based on other questions, it seems like you are using MySQL. So, one method is:

where timestamp >= curdate() and
      hour(timestamp) < hour(now())
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Like Gordon mentioned you need to go with the where clause however looking at your table:

SELECT timestamp, COUNT(*)
FROM table_a
WHERE timestamp >= curdate() and
      hour(timestamp) < hour(now())
Group by timestamp

Should be the complete query.....

Yunfei Chen
  • 630
  • 1
  • 8
  • 20