I have a table with potentially very many records (above 1 million) that contains simple data along with a timestamp that holds the moment of registration (defining CURRENT_TIMESTAMP as the default value for that field).
I want to implement a simple query that scans the table only once and counts the number of records that were register, say, the last 24 hours, the last 7 days and the last month.
Was trying this with no results (getting all zeros though I should get few 1's for sure):
SET @1 = CURRENT_TIMESTAMP() ;
SET @2 = @1 - INTERVAL 24 HOUR ;
SET @3 = @1 - INTERVAL 7 DAY ;
SET @4 = @1 - INTERVAL 1 MONTH;
select Registration_Timestamp , (case Registration_Timestamp when Registration_Timestamp> @2 THEN 1 else 0 end) as LAST_DAY ,
(case Registration_Timestamp when Registration_Timestamp> @3 THEN 1 else 0 end) as LAST_WEEK ,
(case Registration_Timestamp when Registration_Timestamp> @4 THEN 1 else 0 end) as LAST_MONTH
from tbl_Dummy
where Registration_Timestamp >= @4 ;
This is not going to give the sum of course, but once I get this to show 1s I will wrap it with select count(LAST_DAY),count(LAST_WEEK),count(LAST_MONTH) which will do the job of course.
Where is my error here?