SQL concat two fileds into time to calculate TIMESTAMPDIFF - mysql

So I have the following table
Agenda
-hour_begin
-minute_begin
-hour_end
-minute_end
Note: All these fields are varchar(2).
I need to calculate the difference in minutes between (hour_begin/minute_begin) and (hour_end/minute_end)
This is what I have tried so far, but without success.
SELECT TIMESTAMPDIFF(MINUTE,CONCAT(hour_begin.''.minute_begin),CONCAT(hour_end.''.minute_end)) AS diff
This keeps returning an error.
Thanks in advance!

You can calculate the number of hours * 60, then add the number of minutes.
Something like this:
select (60*(hour_end - hour_begin)) +
(minute_end - minute_begin)
from your_table

Related

get average hours in php language

I have a table with 3 columns:
id start_service stop_service
I have already managed to catch the time difference between start_service and stop_service using this query:
SELECT
TIMEDIFF (stop_service, start_service) AS tempo
FROM
user_establishment ORDER BY id;
Now I need to add all the results of this query and divide by the number of records, so as to obtain the average time of all services.
The main problem is the conversion of the hours.
Can someone please help me?
Try this if you need to get your results in hours. You can omit the division by 3600 if you need to have it in seconds (this is what I would do and then manipulate it in my code afterwards).
SELECT
AVG(TIME_TO_SEC(TIMEDIFF(stop_service, start_service)))/3600 AS tempo
FROM
user_establishment;
Hope this helps!
If you want the average time difference in hours upto two decimal points, you can try the below query.
select ROUND(AVG(TIME_TO_SEC(tempo)/3600), 2) as avg_time
from (
select TIMEDIFF(stop_service, start_service) as tempo
from user_establishment
) as timeline
You can modify the average time diff as per your need.
Following query will be used to get average of time difference.
No need to add ORDER BY clause while using AVG() function in mysql query. It saves time to being execute.
SELECT AVG(TIMEDIFF(stop_service, start_service)) AS tempo
FROM user_establishment;

MySQL query to search in between two time range between two dates using timestamp data

I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)

Select in calendar with concat and between

I got a table called calendar, in that table I have this rows:
-day
-month
-year
why? Because I need it hehe.
So, the problem is when I want search a date in diferent year ( in the same year it's ok), for example:
Days between: 31-12-2013 and 1-1-2014, so I have a query:
SELECT * FROM calendar WHERE concat(year,'-',month,'-',day) BETWEEN '2013-1-30' AND '2013-1-31';
But, this query search in the same year and show the another months (I have in my db the 730 days) and not the 2014.
So, I'm confused because, how can I concat the rows and search with between?
Hope you can help me to understand, and sorry for my english.
Thanks for all.!!
Use str_to_date:
SELECT * FROM calendar
WHERE str_to_date(concat(year,'-',month,'-',day),'%Y-%m-%d')
BETWEEN '2013-1-30' AND '2013-1-31';
sqlfiddle demo
Try
SELECT * FROM calendar WHERE DATE(CONCAT(year,'-',month,'-',day)) BETWEEN '2013-1-30' AND '2013-1-31';
I'm also curious as to why you 'need' to store year, month and day as three seperate fields, and not in a single date field?
I think year, month and day may also be reserved keywords in mysql, so you may have to do
SELECT * FROM `calendar` WHERE DATE(CONCAT(`year`,'-',`month`,'-',`day`)) BETWEEN '2013-1-30' AND '2013-1-31';

mysql select, sum, group by date when you have epoch or sysmillis for a time stamp

I have been wrestling around with various time/date manipulations in MySQL and have not figured out how to get this done. I am trying to select a daily sum of a column, volume. My date column contains sysmillis (epoch * 1000). I have tried things such as
SELECT YEAR(from_unixtime(date/1000)) FROM...
and none of what I have tried does the trick. What I want to end up with is a result table that does a sum of all the transactions volume column, for each day. Seems like a pretty simple idea to me, but it just is not working. Is this something that I need to do a nested query to do or should this just be a simple one-liner, that I am just not getting the function right?
SELECT DATE(FROM_UNIXTIME(date/1000)) AS date, SUM(volume)
FROM ...
WHERE ...
GROUP BY date
should do the trick, unless your table structure is wonky.
SELECT SUM(volume) FROM (
SELECT
SUBSTR( FROM_UNIXTIME( ROUND(volumeDate /1000) ), 1, 10) AS dayValue,
volume FROM `volumeTable`
) a
GROUP BY dayValue

Average between two columns in mysql that are date not timestamp

Alright so here it is. I need to figure out the average amount of days between two columns.
Column 1 is recieved_date and column 2 is fix_date
Just want to know how to take the two dates find the difference in days, do that for every row and pop out a number stating the average amount of days it takes to fix something.
Tried to find it online but every time I find something like it, they have two specific dates. I need the entire columns averaged.
You can use the TIMESTAMPDIFF function both for dates and datetime.
See Mysql average time between visits
Add a group by and some other columns to this and it should do the trick:
select
avg(fix_period)
from
(
select
datediff(fix_date, received_date) as fix_period
from some_table
) as a
;