I am trying to get the minutes from each workout from a column and add them up to get the total minutes completed.
I have the command line below:
SELECT sum(timefinished - timestarted) AS minutesBetween FROM workouts WHERE id = 'id'
My SQL database column is formatted as 'time'.
My result to this query is 5239. This is incorrect as the times I need to get a minute value between are:
timefinished: 12:35:02 - timestarted 12:20:19 and timefinished: 12:55:07 - timestarted 12:17:51
The result I am looking for is: 53
I think TIMEDIFF should work in this case. You'll have to use it in conjunction with TIME_TO_SEC:
SELECT SUM(
TIME_TO_SEC(
TIMEDIFF(timefinished, timestarted)
) / 60) AS minutesBetween
FROM workouts
WHERE id = 'id';
Related
I have the time value in one column like below.
Now I need to sum this column and convert the result to minutes.
You can convert the time to seconds and then divide by 60 to get minutes. Something like:
SELECT SUM(TIME_TO_SEC(`activeTime`))/60 FROM tableName
That will give you the decimal version of the minutes (e.g. 5.8 minutes = 5 minutes and 48 seconds). If you want the minutes in time notation (e.g. 05:48):
SELECT SEC_TO_TIME( SUM(TIME_TO_SEC(`activeTime`)) ) FROM tableName
This can help you:
Convert time into seconds,in your case column name is activeTime:
SELECT TIME_TO_SEC('00:12:0');
SELECT TIME_TO_SEC(your_column);
Perform sum operation:
SELECT SUM(output_of_TIME_TO_SEC) AS "total second" FROM table_name;
01) I have a MySQL table named issue. It is storing book details as book_no, book_name, is_date etc.. issued for various members.
02) I need to select list of books that should return on current date. The return date is calculated by adding 3 days to the is_date. I used following script. But I returned an empty result.
Variables used to Store return dates
$rdate=mysql_fetch_array(mysql_query("SELECT * FROM issue"));
$is_date=$rdate['is_date'];
$rt_date=mysql_fetch_array(mysql_query("SELECT
DATE_ADD('$is_date', INTERVAL 3 DAY) FROM dual"));
SELECT statement
SELECT issue.b_no, issue.b_name, issue.is_date
FROM issue WHERE '$rt_date' = CURDATE()
03) I can not understand what I am doing wrong.
Hi i am trying to fetch last 5 minutes of data from oracle table.The query is written below and its not working somehow.
select * from mytable where (time_to_sec(timediff(now(),mytable.time_stamp)) <= 300)
Its showing this error ORA-00904.
I tried one more query.
select * from mytable where TIME_STAMP > (sysdate - numtodsinterval(5,'minute'))
Now, can you tell me the query which fetches data of last 5 minutes and which deletes data that is in the table for more than 12 hours.Thanks.
I need queries in both oracle and mysql. The mysql query i tried is here.
delete from mytable where (time_to_sec(timediff(now(),time_stamp))/3600 >12);
In oracle subtracting 1 from timestamp means one day. And You can substract a fraction of one. So,
current_timestamp - (5/(24*60))
gives You date from 5 minutes ago. Using that we can query:
select * from mytable where TIME_STAMP > current_timestamp - (5/(24*60)
Which should give You needed result. I find this method more straightfoward and simpler to remember than using special functions.
If You want filter out data from last 12 hours than You can query it like this:
select * from mytable where TIME_STAMP <= current_timestamp - 0.5
I am looking to pull scheduled hours in a given time period. Our start and end schedule times are datetimes so I converted them to timestamps. When I dont sum them everything looks correct, but when I sum them over a time period, the output isnt in a timestamp format and the numbers are incorrect.
The query I am using:
select sal.public_name, sum(timediff(timestamp(end_time), timestamp(start_time)))
from bi.support_agents_list sal
join bi.support_sp_shifts_scheduled ss
on ss.agent_sp_id = sal.sp_id
join bi.support_sp_shifts s
on s.pk_id = ss.pk_id
where date(start_time) between '2014-01-29' and '2014-01-31'
group by sal.public_name
A few examples of results I am getting:
Agent 1: 53000 - when it should be 5.5 hours or 5:30
agent 2: 196000 - when it should be 20 hours
Any thoughts on this? I would prefer my output to be in an hour count so 5 hours and 30 min is formatted as 5.5 rather than 5:30.
try this instead of the sum
date_format(timediff(timestamp(end_time), timestamp(start_time)),
'%k hours, %i minutes, %s seconds') as thesum
like that
select sal.public_name,
date_format(timediff(timestamp(end_time), timestamp(start_time)), '%k hours, %i minutes, %s seconds') as thesum
from bi.support_agents_list sal
When doing aggregate calculations with datetime sum(datetime), the result is not what you expect (=cannot sum datetimes). You will be better off converting the datetime to seconds before the aggregate function and then convert it back to time.
Your aggregate function call would then look something like:
select sec_to_time(sum(unix_timestamp(end_time)-unix_timestamp(start_time)))
Be aware that you may reach maximum value that time datatype can contain and that unix_timestamp starts from 1970.
I have a table called "actions" with a DATETIME column called "occurred". I'm trying to return the records for today by doing the following
SELECT * FROM `actions` WHERE `occurred` = DATE(NOW());
But I get an empty result set. If I take the WHERE clause out, I can see all 295 rows in the table and there's at least 30 rows from today. Later I will be writing another query to return all records between today's date and X amount of days in the past but before I can get there I need to know why this query is returning an empty result set.
Thanks in advance.
SELECT * FROM actions WHERE DATE(ocurred) = CURDATE();
DATE(ocurred) ignores the time part.
Here's the SQL Fiddle to play with the data: http://www.sqlfiddle.com/#!2/81708/2
If there in no future date in occurred, you could just use below:
SELECT * FROM `actions` WHERE `occurred` > DATE_SUB(CURDATE(), INTERVAL 1 DAY);