I am trying to display data associated with date. However in my case, I don't want the date to start at 00:00:00 and finishes at 23:59:59. Rather I want it to start at 16:00:00 and finishes at 06:00:00 the next day. In other words I want to create a custom time for date.
In the same time I want to GROUP_BY date.
For instance I have these values in the database:
want it to give me:
date: 2013-09-08 count: 2
date: 2013-09-09 count: 1
I am not asking for code, but a way to think about it, or useful methods.
Thank you in advance!
The simplest method is to take the existing date and subtract six hours to get the "effective" date. You would do this for output purposes only.
Example:
select date(datecol - interval 6 hour) as MyDate, count(*)
from t
group by date(datecol - interval 6 hour);
You can use a where clause to remove the times between 6:00 and 16:00 (unless that is a typo).
Related
I need to calculate the difference between two columns which are of type time. The fields are named start_time and end_time. I use the function timediff(s,e) and it works well but some times it doesn't. For example when end_time is 00:00 which is considered as 12 AM and start_time is 19:00 which is 7 PM the difference function shows 19 hours (actually I also select hour(timediff(s,e))) while I expect it to be 5 hours.
How can I fix this?
I tried this
case
when finish_time < start_time then TIMEDIFF(timestamp('2021:01:02',finish_time),timestamp('2021:01:01',start_time))
else TIMEDIFF(timestamp('2021:01:01',finish_time),timestamp('2021:01:01',start_time))
end as diff,
and it works
As 00:00:00 is considered the start of a new day or even if you have an end time that is the day after the start time you will have to make your start and end field into DATETIME types. Also if you want the difference between the 2 times you should be using timediff(end,start)
I have a MySql DB that consists of a table that holds all my data. I need to retrieve only those rows that have a startup_date that is less than or equal to today at 5 am UTC time. I tried looking up but it's kind of confusing. Can someone provide me a good, clean way of doing this?
My table is as follows:
tbl_MyData
***********
id name city startup_date
1 test1 New York 2020-01-10 18:19:30
2 test2 Houston 2019-01-30 05:00:00
3 test3 Chicago 2020-02-09 05:00:00
From the above data, my query should return ONLY row id number 2, since that is the only row that satisfies my criteria of startup_date being less than or equal to today at 5 am. Also, I'm not sure if I need to have any kind of UTC functions, since I'm not sure how mysql stores it's date time data.
Use a simple WHERE clause
SELECT t.* FROM mytable WHERE t.startup_date <= DATE_ADD(CURDATE(), INTERVAL 5 HOUR)
The CURDATE() function returns the current date without time (hence today at midnight), and the DATE_ADD() function adds 5 hours to it.
What SELECT statement have you tried also what are the data types for the table? If startup-date is a date type in MySQL then it should just be
SELECT * FROM tbl_MyData WHERE startup_date<=(whatever the value needs to be)
So let's say that I want to keep track of my CPU temperature, with simple columns 'date' and 'temperature'. I'd like to see what period saw the highest temperatures on average in the last week. I capture the data every 10 minutes, so I want each 10 minute block averaged with the same block from the other days of the week.
So for example:
2018-01-08 02:00:00 78.3
2018-01-08 03:00:00 81.2
2018-01-09 02:00:00 74.1
2018-01-09 03:00:00 75.9
I would want the averages of each day # 02:00:00, each day # 03:00:00, and so on. (except the real data is every 10 minutes) The exact datetime varies - it's not always 02:00:02, sometimes it could be 02:00:07, etc., so I can't just do an exact conditional.
Any idea how I'd go about making this data? I assume there's some way I can use GROUP BY for this, but I'm lost as to how.
Format just the hour and minute, and group by that.
SELECT DATE_FORMAT(date, '%H:%i') AS time, AVG(temperature) AS temp
FROM yourTable
GROUP BY time
This assumes that the readings are never delayed by more than a minute from the expected 10-minute periods -- if the reading for 02:10 happens at 02:11:01 it will not be put in the group.
I have two dates (start_date and end_date). I am trying to create one record for each month between those two dates.
As an example:
start_date= 01/01/2016
end_date=23/04/2016
Output:
Period
2016-01-31
2016-02-28
2016-03-31
2016-04-30
I am fairly new to mysql and not sure how to achieve that. Could you please give me some ideas?
thanks
select Date_Sub(Date_Add('2015/01/01',INTERVAL 1 Month),INTERVAL 1 Day)
I guess you want to print end date of month between two intervals, Use above statement in your loop.
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