How can i caluculate average HH:MM in SSRS Report.I am getting data with below query
select O.ORDERNUM, Cast(DateAdd(s, AVG(CAST(DateDiff( s, '00:00', cast(DROPTIME as time(0))) AS INT)), '00:00' ) as Time(0)) AS ATIME
FROM ORDE_ O
WHERE
O.CLIENTNUM='HLEX1'
Data is coming like below
OrderNum ATIME
123 16:20:30
124 17:30:00
125 17:56:43
126 17:55:00
Here i want to display Average Hour and Minutes
How can i do this thanks for your help.
I would change the approach slightly. Instead of getting each order's average time and returning a time datatype, just return an integer in seconds. You can then get an average of those integer values and convert back to a time form (if required) in the report itself.
Related
All, I have a MySQL table named duration which contains names and total duration time for separate locations visited over one day. I need to total the times and show them for each person (in hours, mins, seconds format). I am struggling with the correct SQL code (prefer not to use a stored procedure). The Duration column is TIME datatype.
Name Duration
Fred 04:00:20
John 12:39:10
Jack 03:59:20
Stacy 19:17:34
Stacy 03:39:00
John 04:20:30
Jack 00:29:17
John 03:23:50
Fred 300:17:29
I have used the following sql (from Akshay Hegde)
select name, SEC_TO_TIME( SUM( time_to_sec( duration ) ) )
from duration
group by name;
Which works fine for total durations less than 24 hours (i.e. Jack's total duration 04:28:37, Stacy's total duration 22:56:34) but fails for durations greater than 24 hours.
Sadly the sql works fine in MySQL Workbench (even with 300 hours but not within java where the result is "bad format for time '300 in column etc).
I believe I need to cast Duration as a char and then somehow sum on that.
Any assistance appreciated.
Since your Java API doesn't understand times larger than 24 hours, try converting to a string.
select name, CAST(SEC_TO_TIME( SUM( time_to_sec( duration ) ) ) AS CHAR) AS total_time
from duration
group by name;
I have an input form where the user will input their start time and their start time. I then have a function that calculates the time difference and stores the hours and minutes worked in a new field.
What I want to do is calculate the total time as one value in Hours and Minutes.
The following SQL will bring all the meta_value stored in the database
SELECT wp_frm_item_metas.meta_value
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
This returns values as follows (first 5 only)
meta_value
2:15
1:15
2:15
4:15
0.00
If I use the following
SELECT SUM(wp_frm_item_metas.meta_value) AS total_hours
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
I do not get the total in hours and minutes so I wonder if anyone could help me to achieve this please?
Thanks
Wayne
Use TIME_TO_SEC to convert to seconds, then SUM. For output you could use SEC_TO_TIME function to get hours:mins. E.g:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(wp_frm_item_metas.meta_value))) AS total_hours
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
I haven't tested the above but should get you close...
I want to create a query in Access that will sum up the total of time giving per project.
I have a database that look like:
Time | Project
------------------
1:00 | 1455
2:30 | 1666
0.50 | 1455
0.45 | 1455
I want to know how many hours were given to all project.
So far I have created a query that return the total of hours per project, which is good.
However, the format of the time is odd.For instance a project that should return 17:10 hours return this 0.715277777777778
I am trying to format the field Time so the total return a normal looking format like 17:10 hours instead of all those decimals.
This is the SQL I have right now
SELECT Sum(Dan.Time) AS SumOfTime, Dan.Project
FROM Dan
GROUP BY Dan.Project
ORDER BY Dan.Project;
Can anyone help?
You can use format function:
Format(Sum(Dan.Time),"hh:nn") AS SumOfTime
P.S. DateTime in ms Access are in fact double where 1 is one day, and 1/24 is one hour and so on.
I'm not working with ms access, but maybe this helps you.
All times should be saved in the same type. Maybe you should format the times before storing them in the database.
If it is not possible, you can try this
SELECT
Int(Sum(Dan.Time) * 24) & ":" & (Int((Sum(Dan.Time) * 24 - Int(Sum(Dan.Time) * 24)) * 60) + 1)
AS TimeStr,
Dan.Project
FROM
Dan
GROUP BY
Dan.Project
ORDER BY
Dan.Project;
It computs the hours and the minutes from the decimal number and concatenates the numbers as HH:MM.
Maybe there is also a better build in way in ms access.
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 need to select a room rate in which check-in date and check-out date is between a range of date specify. Those rates are named separately according to its conditions. Room costs are depends on the date selected. Here is my code:
rate_eb
rate_name rate_starts rate_ends rate_discount rate_min_stay
---------------------------------------------------------------------------
Low Season 2013-05-01 2013-10-31 20 3
High Season1 2013-11-01 2013-12-19 20 4
High Season2 2013-02-21 2013-04-31 20 4
Peak Season 2013-12-20 2014-02-20 20 5
The conditions are:
A booking must be in between rate_starts and rate_ends.
A total of nights stay must be greater or equal to rate_min_stay.
rate_discount is a percentage of discount from a master rate from another table. Saying if a master rate is 100, a rate of 80 will be applied to this booking.
Now, I'd like to get those data from rate_db with a date range - especially for rate_discount. Here's my mySQL:
select rate_discount
from rate_eb
where rate_min_stay<='4'
and reb_starts>='2013-06-19'
and reb_ends<='2013-06-23'
From the code above. I expect rate_discount=20 from Low Season but all of the rate less than or equal to 4 are selected.
Now, please suggest me the solutions. How I can re-write my code to access the rate_discount between rate_starts and rate_ends.
I assume that the visitor can enter a period, not just one date. What happens if the startdate of that period is in low season and the enddate in high season? Which rate would you like to see then?
select rate_discount
from rate_eb
where rate_min_stay <= abs( datediff( reb_date2, reb_date1 ) )
and reb_date1 between rate_starts and rate_ends
and reb_date2 between rate_starts and rate_ends
I'm assuming that reb_date1 is the startdate the visitor entered, and reb_date2 the enddate.
use to_date('dateInString','format') to convert string to date for reb_date1 and reb_date2 and use unquoted number value directly if you are doing a greater than comparision for rate_min_stay