MariaDB EVENT autoclear DB returns warning - mariadb

I got MariaDB version 10.3.27 where im trying to run EVENT which starts everyday at 1am for clearing some databases. but when I try to pass lines below, it return 1 warning, but Im not sure why....
Can someone please clarify this for me? Thanks
delimiter $$
CREATE EVENT AUTOCLEAR
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(NOW()) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
BEGIN
DELETE FROM ESP1 WHERE timestamp < ((UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))) + 7200);
DELETE FROM ESP2 WHERE timestamp < (UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) + 7200);
DELETE FROM ESP3 WHERE timestamp < (UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) + 7200);
DELETE FROM ESP3_1 WHERE timestamp < (UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) + 7200);
END$$
delimiter ;

Sorry my bad. Didnt enabled scheduler and not know how to read warnings...
SET GLOBAL event_scheduler=ON
fixed my problem

Related

How to compare timestamp with time using pl/sql?

I have parameter :dateSend which has type timestamp and I am trying to make this evaluation.
CASE WHEN :dateSend <= TO_TIMESTAMP('14:00:00', 'HH24:MI:SS') THEN 0 ELSE 1 END A
It doesn't work. Maybe I should convert :dateSend to time? Like to_char(CAST(:dateSend AS DATE),'hh24:mi:ss') but it doesn't work.
I need to show 0 if time from :dateSend is smaller then 14.00.
It appears that you want to obtain records having a time component which is earlier than 14:00 hours (2pm). If so, then use this:
SELECT
CASE WHEN EXTRACT(hour FROM :dateSend) < 14 THEN 0 ELSE 1 END AS A
FROM yourTable;

Complex calculation of the hours amount

I have a Pythonic system that stores student absences data in a SQLite database. Each row includes the start and end time of the absence, represented by the number of seconds since Jan 01 1970. I was asked to add a feature which limits the number of hours of absence per week.
It sounds easy to pull out the amount of hours, using a statement like this:
SELECT (sum(ending-starting)/3600)
FROM requests
WHERE student_id = {x}
AND starting BETWEEN {y} AND ({y}+604800)
AND approved = 1
The problem is that the limit must only be the hours defined as "mandatory presence." For example, if a user has defined the hours 8:00 to 17:00 as a "mandatory presence," an absence that begins on Sunday at 14:00 and ends on Monday at the same time, will be calculated in the code above 24 hours, while in practice it is only 9 hours.
"Mandatory presence" is defined in the database as two numerical parameters: "morning" and "evening" (always a round hour). Is there a way to make the calculation above taking into account these two numbers?
If it can not be done in sql, I would love to hear how to select the data in sql and then perform the calculation in python.
I believe the following may do what you wish :-
SELECT
(
sum((
(ending - starting)
-(
CASE WHEN starting < strftime('%s',date(starting,'unixepoch')||' 08:00')
THEN strftime('%s',date(starting,'unixepoch')||' 08:00') - starting
ELSE 0
END
+
CASE WHEN ending > strftime('%s',date(starting,'unixepoch')||' 17:00')
THEN ending - strftime('%s',date(starting,'unixepoch')||' 17:00')
ELSE 0
END
)
) /3600)
) AS ha, *
FROM requests
WHERE student_id = {x}
AND starting BETWEEN {y} AND ({y}+604800)
AND approved = 1
;
MikeT's answer is not entirely working, but it certainly helped me reach the desired result. Here's the perfect statement:
SELECT
(
sum((
(ending - starting)
-(
CASE WHEN starting < strftime('%s',date(starting,'unixepoch')||printf(' %02d:00', morning))
THEN strftime('%s',date(starting,'unixepoch')||printf(' %02d:00', morning)) - starting
ELSE 0
END
+
CASE WHEN ending > strftime('%s',date(ending,'unixepoch')||printf(' %02d:00', evening))
THEN ending - strftime('%s',date(ending,'unixepoch')||printf(' %02d:00', evening))
ELSE 0
END
)
) /3600.0
-(
(24-evening+morning)
*
(round(julianday(ending, 'unixepoch'))-round(julianday(starting, 'unixepoch')))
)
)) AS ha
FROM requests
INNER JOIN students ON requests.student_id = students.ID
INNER JOIN institutes ON students.inst_id = institutes.ID
WHERE student_id = {x}
AND starting BETWEEN {y} AND ({y}+604800)
AND approved = 1;
Thank you very much for your help!

How to get number of records created in last 2 minutes

I have successfully get the records crated in today using following code.
$carcount = Car::where('username', '=', $username)
->whereRaw('created_at >= CURRENT_DATE')
->whereRaw('created_at < CURRENT_DATE + INTERVAL 1 DAY')
->count();
but i canot get the records crated in last minute using following code.
$carcount = Car::where('username', '=', $username)
->whereRaw('created_at >= CURRENT_DATE')
->whereRaw('created_at < CURRENT_DATE + INTERVAL 1 MINUTE')
->count();
Please help me..
CURRENT_TIMESTAMP is what you need. You are adding 1 minute to the beginning (midnight) of the current day.

rails 3 group by date a datetime column

Foo.group(:start_at).count(:id)
How I can group this by date ? the "start_at" column is an datetime column
This should work (rails 3):
Foo.order(:start_at).group("DATE(start_at)").count
edit: if you're using PostgreSQL, the query should be
Foo.order("DATE(start_at)").group("DATE(start_at)").count
or you'll get an error
("PGError: ERROR: column "foos.start_at" must appear in the GROUP BY clause or be used in an aggregate function")
Based on
Graphing new users by date in a Rails app using Seer
and
http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/
I created a gem for this. https://github.com/ankane/groupdate
Foo.group_by_day(:created_at).count
You can even specify a time zone.
Foo.group_by_day(:created_at, time_zone: "Pacific Time (US & Canada)").count
This is another way to solve this if you manage different time zones. The idea is to add or substract the hours of your timezone:
hours_diff = Time.zone.now.time_zone.utc_offset/(60*60) rescue 0
hours_diff = hours_diff.to_i
date_group = "DATE(form_answers.created_at)"
if hours_diff > 0
date_group = "DATE(DATE_ADD(form_answers.created_at, INTERVAL #{hours_diff} HOUR ))"
elsif hours_diff < 0
date_group = "DATE(DATE_SUB(form_answers.created_at, INTERVAL #{hours_diff.abs} HOUR ))"
end
Foo.group(date_group).count

MySQL: Records inserted by hour, for the last 24 hours

I'm trying to list the number of records per hour inserted into a database for the last 24 hours. Each row displays the records inserted that hour, as well as how many hours ago it was.
Here's my query now:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY time ASC
right now it returns:
28 23
62 23
14 20
1 4
28 3
19 1
That shows two rows from 23 hours ago, when it should only show one per hour.
I think it has something to do with using NOW() instead of getting the time at the start of the hour, which I'm unsure on how to get.
There must be a simpler way of doing this.
If you grouped by HOUR(time) then you should use HOUR(time) in your select expressions, and not time. For example:
SELECT HOUR(time), COUNT(*)
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY HOUR(time)
Alternatively you can group by the expression you want to return:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
ORDER BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
In case you were wondering, it is safe to call NOW() multiple times in the same query like this. From the manual:
Functions that return the current date or time each are evaluated only once per query at the start of query execution. This means that multiple references to a function such as NOW() within a single query always produce the same result.

Resources