I have a problem when I try get the total number of minutes in 2 rows. For example:
I don't know in mysql how to calculate this time with a query.
I want get minutes of 1 rows include time start and time end to use it.
your query...
SELECT ((HOUR('time end') * 60) + MINUTE('time end')) - ((HOUR('time start') * 60) + MINUTE('time start'))
Related
In this query which I got help from #jarlh, in this question ORDER BY CASE
SELECT *
FROM booking
WHERE booking_date=$b_date OR DATE(delivery_date)=$b_date
ORDER BY CASE WHEN booking_date=$b_date
THEN hour * 60 + minute
WHEN DATE(delivery_date)=$b_date
THEN HOUR(delivery_date) * 60 + MINUTE(delivery_date)
END ASC
I need to find those posts with the same HOUR and MINUTE, before I leave the query, and mark them somehow, so I’m able to print them (those in the same hour and minute) horizontally and not vertically.
I am looking for a way to be able to grab rows from a database from a time.
Two columns exist:
Time - scheduled Time
Delay - +seconds that time is delayed.
Let's say:
ID,time,delay
1,18:23,360
2,18:25,0
3,17:15,-60
Now, let's say I am searching for buses after actually arriving at/after 18:25, how would i do it to include these two results:
2,18:25,0
1,18:23,360 (note: 18:23 + 360 seconds = 18:29)
in a query like
where `time` >= '18:23'
Thanks
SELECT * FROM [TABLE] WHERE DATE_ADD(Time,INTERVAL TimeDelay SECOND) >= '18:25:00'
MySql Date_Add Function
Mysql Table:
I want to take sum of acceleCount for 2 minutes intervals.
Query:
select time div 120000 as TwoMinutes,
sum(acceleCount) as Sum
from acceleTable
group by time div 120000
Result:
Here the twoMinutes column timestamp is meaning less. I want it to have a timestamp which is within the considering two minutes.
Any thoughts on how to change the sql query?
Bring the timestamps to a common denominator by dividing, rounding and multiplying:
SELECT
(ROUND(time / 120) * 120),
sum(acceleCount)
FROM acceleTable
GROUP BY (ROUND(time / 120) * 120)
A little optimized way to do it
SELECT (ROUND(time / 120000) * 120000) AS timekey, sum(acceleCount)
FROM acceleTable
GROUP BY timekey
I am trying to work out how to return a random time between 2 time ranges.
For example, get a random time between 15:00:00 and 22:00:00
So far, I have - but don't know how to limit it
select sec_to_time(floor(15 + (rand() * 86401)))
Any help would be much appreciated!
SELECT SEC_TO_TIME(
FLOOR(
TIME_TO_SEC('15:00:00') + RAND() * (
TIME_TO_SEC(TIMEDIFF('22:00:00', '15:00:00'))
)
)
);
Calculate in seconds. Then add a random number that is in the range of seconds between 15:00:00 and 22:00:00 to 15:00:00. Then convert the seconds back to a time value.
for more information on the used functions, see this link.
Logic is simple,
Get difference between 2 times.
Generate random number between those 2 times(numbers)
You can direct generate time in Minutes or Seconds.
Following is example for getting Random Hour,
select Round((FLOOR(RAND() * (TIME_TO_SEC('15:00:00') - TIME_TO_SEC('22:00:00') + 1)) + TIME_TO_SEC('22:00:00')) / 3600,0)
EDIT : Fiddle
For reference see this link
Try this way
select FROM_UNIXTIME(UNIX_TIMESTAMP('<starting_date_in_date_format>') + FLOOR(0 + (RAND() * UNIX_TIMESTAMP('<ending_date_in_date_format>'))) from dual;
I have complicated query over very big table.
Long story short, when I use convert time to select period of day (let's say 12-13h, converting it from datetime row) query takes few minutes, instead of few seconds without convert!
So, I tried datepart, and it works well, almost instant, but, problem is, how to point to hours and minutes in same time?
Any other fast solution is more than welcome.
Thanks.
Meanwhile I came up with this:
DATEPART(HOUR, datetimecolumn)*100 + DATEPART(MINUTE, datetimecolumn)) between 1210 and 1540
You can use datePart if you are willing to do a bit of math, as shown below:
12:10 = 12 * 60 + 10 = 730 minutes
15:40 = 15 * 60 + 40 = 940 minutes
select * .....
where datepart(mi, datefield) between (12*60+10) and (15*60+40)
If you have a constant periods - i.e. - always hourly and no any floating periods - you may introduce something like "ordinal number of period" calculated field, index on it and query of it with precalculated period value
OR
is there are no any constant periods - try to calculate proper begin and end values prior to SELECT statement and use them in the query.
Keep in mind that using functions in where clause of query - sometimes is a bad idea. Using functions in ORDER BY clause - always bad
You can get GETTIME from following Function
alter function GetTimeOnly(#_DateTime DateTime)
returns datetime
as
begin
return dateadd(day, -datediff(day, 0, #_datetime), #_datetime)
end
go
OR YOU CAN HAVE THE TIME FROM CONVERT FUNCTION.
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly