Say I have a column
Date
23-03-2019
04-04-2019
I want to find hoe many minutes the whole month has in MySQL.
Expected output:
Date MinsinMonth
23-03-2019 44640
04-04-2019 43200
Basically, you just want to find the number of days in the month and then do some multiplication. For this, use last_day():
select day(last_day(date)) * 24 * 60 as minutes_in_month
This should work:
SELECT DAY(LAST_DAY(Date)) * 1440 AS MinsinMonth
LAST_DAY returns the last day in the month a date is in
DAY Returns the day number associated to a date
1440 is the number of minutes per day (60 * 24)
Related
I need to get all rows, by an ISO datetime field, EVERY "n" (eg. 180) days (note: NOT only the last 180 days).
For example, I need to select every single row at 180 days, 360 days, and so on.
So, for example, if I have a record with date 2016-01-11 12:30:00, I'll get this record when DATE() is 2016-07-09, 2017-01-05, 2017-07-04, and so on.
Use DATEDIFF function of MySQL
Demo: http://rextester.com/AEIN10581
set #QUERY_DATE='2016-07-09';
SELECT * FROM `dates`
WHERE DATEDIFF(`date`, #QUERY_DATE) % 180 = 0;
In place of #QUERY_DATE, you can use CURDATE() for current date.
i have column datetime format with consecutive dates, but i need count 60 days ago from now but i need prevent days marked with 0 because days are sundays or holidays.
this not work:
SELECT DateDVH as refDays
FROM DVH_days
WHERE DateDVH>'2016-12-20 00:00:00'
AND (DateDVH_dia<'2017-02-18 00:00:00' + INTERVAL 60 DAY)
AND DateDVH_daytype!=0
DateDVH_daytype=0 is holidays and sundays.
It is possible to place a counter in the query or a condition that prevents the days type = 0 or a stored procedure??
update
i solve it with :
SELECT DateDVH_dia
FROM Avipac_DVH_dias
WHERE DateDVH_dia>now()
AND DateDVH_diatype!=0
LIMIT 60
but i get a list of 60 date and only need get the last date
I have statistical data like this:
time val1
1424166578 51
1424166877 55
1424167178 57
1424167477 57
time is a unix timestamp. There is one record every 5 minutes excluding nights and sundays. This continues over several weeks.
Now I want to get these values for an average day and an average week. The result should include values for every 5 minutes like normal but for average past days or weeks.
The result should look like this:
time val1
0 43.423
300 46.635
600 51.887
...
So time could be a timestamp with relative time since day or week start. Perhaps it is better to use DATETIME... not sure.
If I use GROUP BY FROM_UNIXTIME(time, '%Y%m%d') for example I get one value for the whole day. But I want all average values for all days.
You seem to be interested in grouping dates by five minute intervals instead of dates. This is fairly straightforward:
SELECT
HOUR(FROM_UNIXTIME(time)) AS HH,
(MINUTE(FROM_UNIXTIME(time)) DIV 5) * 5 AS MM,
AVG(val1) AS VAL
FROM your_table
WHERE time > UNIX_TIMESTAMP(CURRENT_TIMESTAMP - INTERVAL 7 DAY)
GROUP BY HH, MM
The following result will explain how date is clamped:
time FROM_UNIXTIME(time) HH MM
1424166578 2015-02-17 14:49:38 14 45
1424166877 2015-02-17 14:54:37 14 50
1424167178 2015-02-17 14:59:38 14 55
1424167477 2015-02-17 15:04:37 15 00
I would approach this as:
select date(from_unixtime(time)) as day, avg(val)
from table t
group by date(from_unixtime(time))
order by day;
Although you can use the format argument, I think of that more for converting the value to a string than to a date/time.
I have most of my datetime dimensions defined but having trouble with these. Have 0 idea how to implement these in mysql for creating my datetime dimensions.
Minute of day - 1440 minutes span.
Day per Bi-weekly - 14 days span - from 1st week of year, starting at 1st day of year.
Day per quarter - 4 quarters per year, so within those quarters need day count like 1st day of quarter, 2nd day, etc.
Week per quarter - Same like day per quarter but at the week level.
One way i can slve these is by using CASE statements for each time interval but that is too much, like (if hour = xx & minute = xx then minuteOFDay = xxxx). Same for the other data but I am sure there must be a 1-2 line formula to get these instead?
For the "minute of day", you can use this (assuming that you have your data as DATETIME named eventdt):
SELECT (HOUR(`eventdt`) * 60) + MINUTE(`eventdt`)
AS minute_of_day FROM `your_table`
For "day per bi-weekly", day of year modulo 14 might be usable:
SELECT (DAYOFYEAR(`eventdt`) % 14) AS day_per_biweekly
FROM `your_table`
How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
Not entirely related to this question but is related to the title:
SELECT SUBTIME("10:24:21", "5"); -- subtracts 5 seconds. (returns "10:24:16")
SELECT SUBTIME("10:24:21", "01:00:00"); -- subtracts one hour. (returns "09:24:21")
Documentation: MySQL SUBTIME function
Use:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
You can use this :
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
In order to subtract the time instead, say 15 minutes, the following should work:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
Yes its possible using date function in Mysql
select distinct
lastname,
changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I have subtract 15 days from my date - check image please. thanks