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;
Related
I am using the query
SELECT GridPow
, DATE_FORMAT(Time,'%H')
, DATE_FORMAT(Time,'%i')
, INVId
from KStar_Data
where Date=?
and SiteId=?
and INVId=?
order
by HOUR(Time)
, MINUTE(Time);
How to get the data only for each 15min data.
First, you have to define which data you want to group each 15 minutes.
You can ask for this like:
SELECT
somefield,
SUM(myfield)
from mytable
where some=thing
GROUP BY HOUR(Time),
FLOOR(ROUND(MINUTE(Time)/15.5,1));
This way you get four groups depending on the minutes:
0: 0-14 min
1: 15-30 min
2: 31-45 min
3: 46-59 min
That's not fully accurated as you get two groups of 15 minutes, one of 16 minutes and one of 14 minutes, but it could do the trick. Or perhaps you can play with the 15.5 value to get a more accurated formula if you need exact 15 minutes each group.
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';
My DB values save in this format
"12:07 AM"
dataType varchar in Sql Server
i want minutes b/w 2 times 2nd time is
current time
23:50 AM
23:24:43.6100000
differnce 26 minutes
but output come in negative values by executing below query
here m trying to do but output come in negative values
select datediff(minute,cast(getdate() as time),OrderDeliverTime)
from tblOrder
If the OrderDeliverTime occurs in the past, then it should be the middle parameter in DateDiff:
select datediff(minute,OrderDeliverTime,cast(getdate() as time)) from tblOrder
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 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