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;
Related
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.
I've just started a job and my boss wants me to learn mySQL so please bear with me, i've been learning for only 2 days and i'm not that good at it yet.
So i've been given 3 tables and several tasks to do.
The tables are:
mobile_log_messages_sms
mobile_providers
service_instances
And in them i've got to:
Find out how many messages there were in the last 25 days and how
much income did they make
Then i need to group them by day (so per day, exclude hours) and
provider name.
Also i need to ignore all the messages that have an empty string
under the service column
Also i need to ignore the messages that made 0 income and count only
those that have the column service_enabled = 1
And then i need to sort it descending, by date.
in the tables
mobile_log_messages_sms:
message_id - used to count the messages
price - using for price obviously, exlude those with 0
time - date in yyyy/mm/dd hh:mm:ss format
service - exclude all those that have an empty string (or null)
mobile_providers
provider_name - to use to group with
service_instances
enabled - only use if value is 1
I've started with:
SELECT message_id, price, time
FROM mobile_log_messages_sms
WHERE time BETWEEN '2017-02-26 00:00:00'
AND time AND '2017-03-22 00:00:00'
But i need to change the date format and then use the JOIN commands but i don't know how, and i know i need to add more to it, but i'm stumped even at the start. Also the starting just lists the messages but i need to count the total sum of the income (price) per day.
Can anyone point me in the right direction at least since i'm still a noob? Many thanks in advance and sorry if i worded something badly, english is not my first language.
Find out how many messages there were in the last 25 days and how much income did they make
1.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE;
2.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE
GROUP BY CAST(time AS DATE);
3.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE AND service IS NULL
GROUP BY CAST(time AS DATE);
rest can't done with join so make sure that at least one column should be common in tables.
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
I'm trying to do a lookup on our demographioc table to display some stats. However, since out demographic table is quit big I want to do it in one query.
There are 2 fields that are important: sex, last_login
I want to be able to get the total number of logins for various date ranges (<1day ago, 1-7 days ago, 7-30 days ago, etc) GROUPED BY sex
I right now know how to do it for one date range. For example less than 1 day ago:
SELECT sex, count(*) peeps
FROM player_account_demo
WHERE last_demo_update > 1275868800
GROUP BY sex
Which returns:
sex peeps
----------------
UNKNOWN 22
MALE 43
FEMALE 86
However I'd have to do this once for each range. Is there a way to get all 3 ranges in there?
I'd want my end result to look something like this:
sex peeps<1day peeps1-7days peeps7-30days
Thanks!
IMPORTANT NOTE: last demo_update is the epoch time (unix time stamp)
SELECT sex,
SUM(IF(DATEDIFF(NOW(),last_login) < 1,1,0)),
SUM(IF(DATEDIFF(NOW(),last_login) BETWEEN 1 AND 7,1,0)),
SUM(IF(DATEDIFF(NOW(),last_login) BETWEEN 7 AND 30,1,0))
FROM player_account_demo
GROUP BY sex
You want to use a Pivot Table.