I'm trying to write a sql query that gives me the average time.
I have 5 columns and multiple entries.
the five columns are taxi-number start-date end-date start-time end-time.
I need to find the average time, times given are in 24 hour format but I also need to account for the difference in start-date and end-date if its there,and i need to group it by taxi number.
input
Taxi-Number Start-Date Start-Time End-Date End-Time
4412 8/8/2015 18:06:00 8/9/2015 14:00:00
2223 4/18/2013 19:33:00 4/19/2013 0:40:00
1112 10/20/2013 11:23:00 10/22/2013 8:33:00
5553 5/18/2015 21:43:00 5/19/2015 9:15:00
2222 4/9/2014 0:00:00 4/10/2014 0:16:00
output
taxi 4412, average time 20 hours 6 minutes or in any format that accounts for this
Thanks.
Not an answer. Too long for a comment...
Start here:
Taxi_Number Start_Date End_Date
4412 2015-08-08 18:06:00 2015-08-09 14:00:00
Once you have that as your structure, see Why should I provide an MCVE for what seems to me to be a very simple SQL query? ... and then get back to us.
Related
I am creating a clock-in time system and so far I have been able to get user clock in time for today and user clock in time for the current week.
The final step is to get user current time for the current pay period.
I have created a list of pay period start & end dates in Excel.
Whenever you use a function like Excel WEEKNUM() or MySQL YEARWEEK(), these functions come with an additional option parameter.
The links below show the differences between these modes in a table.
Excel WEEKNUM() table reference
MySQL YEARWEEK() table reference
My question is, if we do payroll biweekly, which mode do I set in Excel WEEKNUM() that corresponds to MySQL YEARWEEK()?
Attached spreadsheet clock.logic.xlsx
Thank you for any help.
At first the good news: The Excel ISOWEEKNUM function corresponds to the MySQL WEEKOFYEAR which is WEEK(date,3). So determining ISO week numbers is possible.
But all other WEEK modes are simply crap because the definition of the first week in year does not fit any logic used elsewhere. For example, take the simplest mode having Sunday as the first day of the week and the first week of the year is the week, the first day of the year falls in. This is what Excels WEEKNUM function returns with Return_type 1 or omitted. This should be MySQLs WEEK in modus 0 (0-53) or 2 (1-53). But what the heck?
SELECT WEEK('2008-01-01',0); -> 0
SELECT WEEK('2008-01-01',2); -> 52
So MySQL tells us, Tuesday, 2008-01-01, is in week 52 of 2007?
Really? Why?
Because the rule "Week 1 is the first week … with a Sunday in this year" is not fulfilled by MySQL. Instead it seems for MySQL the first week starts with the first Sunday in this year.
So except of the ISO week numbers, all other week numbers from MySQL are wrong. One could think: Let us take modus 0 and simply add 1 to the result. But that fails in 2012. Because there 2012-01-01 is Sunday and there MySQL gives week number 1 in modus 0 as well as in modus 2.
Examples:
Excel:
Date WEEKNUM ISOWEEKNUM
2008-01-01 1 1
2008-02-01 5 5
2008-02-03 6 5
2008-02-04 6 6
2008-12-31 53 1
2009-01-01 1 1
2009-02-01 6 5
2009-12-31 53 53
2012-01-01 1 52
2012-02-01 5 5
2012-12-31 53 1
2016-01-01 1 53
2016-02-01 6 5
2016-12-31 53 52
MySQL:
drop table if exists tmp;
create table tmp (d date);
insert into tmp (d) values
('2008-01-01'),
('2008-02-01'),
('2008-02-03'),
('2008-02-04'),
('2008-12-31'),
('2009-01-01'),
('2009-02-01'),
('2009-12-31'),
('2012-01-01'),
('2012-02-01'),
('2012-12-31'),
('2016-01-01'),
('2016-02-01'),
('2016-12-31');
select d as 'Date', week(d,0), week(d,3) from tmp;
Result:
Date week(d,0) week(d,3)
2008-01-01 0 1
2008-02-01 4 5
2008-02-03 5 5
2008-02-04 5 6
2008-12-31 52 1
2009-01-01 0 1
2009-02-01 5 5
2009-12-31 52 53
2012-01-01 1 52
2012-02-01 5 5
2012-12-31 53 1
2016-01-01 0 53
2016-02-01 5 5
2016-12-31 52 52
If you want to calculate hours in current pay period in Excel, given a two week pay period, then I'd suggest that you don't need week numbers at all (in fact that overcomplicates the calculation, especially at the start or end of the year)
If you have dates in A2:A100 and hours worked on those dates in B2:B100, and a list of pay period start dates in Z2:Z10 then you can get hours in current pay period with this formula
=SUMIF(A2:A100,">="&LOOKUP(TODAY(),Z2:Z10),B2:B100)
I imagine your actual setup is more complicated, but some variation on the above can probably still be used
So let's say that I want to keep track of my CPU temperature, with simple columns 'date' and 'temperature'. I'd like to see what period saw the highest temperatures on average in the last week. I capture the data every 10 minutes, so I want each 10 minute block averaged with the same block from the other days of the week.
So for example:
2018-01-08 02:00:00 78.3
2018-01-08 03:00:00 81.2
2018-01-09 02:00:00 74.1
2018-01-09 03:00:00 75.9
I would want the averages of each day # 02:00:00, each day # 03:00:00, and so on. (except the real data is every 10 minutes) The exact datetime varies - it's not always 02:00:02, sometimes it could be 02:00:07, etc., so I can't just do an exact conditional.
Any idea how I'd go about making this data? I assume there's some way I can use GROUP BY for this, but I'm lost as to how.
Format just the hour and minute, and group by that.
SELECT DATE_FORMAT(date, '%H:%i') AS time, AVG(temperature) AS temp
FROM yourTable
GROUP BY time
This assumes that the readings are never delayed by more than a minute from the expected 10-minute periods -- if the reading for 02:10 happens at 02:11:01 it will not be put in the group.
My DB in UTC timezone and data inserting in UTC time. I want to sum all values and group by hourly for IST time data. like below,
id data_id value serverTime
1 2 100 2016-05-02 18:30:54
2 2 100 2016-05-02 18:45:54
4 2 200 2016-05-02 19:00:54
5 2 100 2016-05-02 19:15:54
6 2 100 2016-05-02 19:30:54
7 2 100 2016-05-02 19:40:54
Query
select sum(value) as value, serverTime as date
from Data_table
where data_id=2
and serverTime between CONVERT_TZ('2016-05-03 00:00:01','+00:00', '-05:30')
and CONVERT_TZ('2016-05-03 10:45:24','+00:00', '-05:30')
group by year(date),month(date),week(date),day(date),hour(date);
above query giving result is :
200
500
But Expecting output :
600
100
because IST 12 AM = UTC- 05:30 which means 18:30 to 19:30 but here my query calculating only 18:30 to 19:00, 19:00 to 20:00, 20:00 to 21:00 which is not accuracy value.
I want to calculate value for 18:30 to 19:30 and 19:30 to 20:30 for accuracy value for IST time data.
How to solve this?
By IST, I assume you mean India Standard Time, which is 5 hours and 30 minutes ahead of UTC. As a fixed offset, that would be +05:30, not -05:30. You're results are incorrect because you have the sign inverted.
The CONVERT_TZ function accepts any of:
'SYSTEM' for the local system time zone
Fixed offsets in standard ISO 8601 format, which have positive offsets East of UTC, such as '+05:30' for India, or '-10:00' for Hawaii.
Named time zones, using standard IANA/Olson TZDB identifiers, assuming the time zone tables are populated. India's is 'Asia/Kolkata', US Eastern time is 'America/New_York', etc. Using this option requires the mysql time zone tables to be populated, per the documentation.
In general, named time zones are preferred because they accommodate changes in offset due to daylight saving time and historical changes. However, India has been fixed at +05:30 since 1942 and isn't likely to change in the near future, so it's reasonable to use the fixed offset approach if this is the only time zone you need to deal with.
Also note that "IST", like many time zone abbreviations, is ambiguous. It can mean India Standard Time (+05:30), Ireland Standard Time (+01:00) or Israel Standard Time (+02:00). Also note that Ireland Standard Time is actually a daylight time zone offset, despite having the name "Standard" in it. To avoid confusion, please specify your particular location when referring to IST in the future, and don't ever expect a computer to be able to distinguish them.
You should convert also date in group by
select sum(value) as value , hour(CONVERT_TZ(serverTime, '+00:00', '-05:30')) as hour
from Data_table
where data_id=2
and serverTime between CONVERT_TZ('2016-05-03 00:00:01','+00:00', '-05:30')
and CONVERT_TZ('2016-05-03 10:45:24','+00:00', '-05:30')
group hour(CONVERT_TZ(serverTime, '+00:00', '-05:30'));
Test for between condition
select CONVERT_TZ('2016-05-03 00:00:01','+00:00', '-05:30') ,
CONVERT_TZ('2016-05-03 10:45:24','+00:00', '-05:30') from dual;
select CONVERT_TZ('2016-05-03 00:00:01','+00:00', '-05:30') ,
CONVERT_TZ('2016-06-03 01:00:24','+00:00', '-05:30') from dual;
How can I select all rows from a table where a date column is within a specific range of dates, at a given period (e.g. every 14 days)?
The table has a date column with most every date represented, possibly multiple times. The range is defined by a start date and an end date. The period is a number of days. For example:
Start: 2016-01-01 (friday)
End: 2016-12-31 (saturday)
period: 14 (days)
For the above, the query should return rows for every other Friday in 2016. That is, it should return the rows for the following dates:
2016-01-01
2016-01-15
2016-01-29
2016-02-12
2016-02-26
2016-03-11
2016-03-25
2016-04-08
2016-04-22
2016-05-06
2016-05-20
2016-06-03
2016-06-17
2016-07-01
2016-07-15
2016-07-29
2016-08-12
2016-08-26
2016-09-09
2016-09-23
2016-10-07
2016-10-21
2016-11-04
2016-11-18
2016-12-02
2016-12-16
2016-12-30
Currently, this is done in a stored procedure where a loop fills a temp table with the target dates, which is later joined on. However, I am trying to rewrite this code to step away from stored procedures.
What would be the best way to get the desired rows without using the stored procedure & a temp table? Keep in mind that (one of) the table(s) is quite large at around 1M records indexed on date, so any calculated values might impact the performance severely.
Alternatively, I could calculate all dates in the interval in PHP/RoR and use a massive IN clause, but hopefully there is a better solution.
Try this:
table_name1 is your table
date1 the date field
"2022-01-02" the start (twice included)
"2022-01-10" the end
3 the interval
SELECT date1
FROM table_name1
WHERE date1 BETWEEN "2022-01-02" AND "2022-01-10"
AND (DATE("2022-01-02") - date1) % 3 = 0;
Tested it with MySQL 5.6.
I am creating a REST API for a booking calendar, and right now I am trying to figure out the most efficient way of writing a query that returns all timestamps between two dates with a 15 minute interval. If I supply2013-09-21 and 2013-09-22 I would like to get:
2013-09-21 00:15:00
2013-09-21 00:30:00
2013-09-21 00:45:00
2013-09-21 01:00:00
2013-09-21 01:15:00
2013-09-21 01:30:00
...
2013-09-22 23:15:00
2013-09-22 23:30:00
2013-09-22 23:45:00
I would then use this query as a subquery and apply some conditions on it to remove timeslots outside working hours (which are not constant), booked timeslots, etc.
I have seen a lot of blog posts where the author creates a "calendar table" which stores all these timestamps, but that seems like a waste to me since that data doesn't need to be stored.
Any suggestions on how I could do this or a better way to fetch/store the data?
Here is a process that generates 95 rows incrementing a date variable as it goes and then left join the table with the dated entries to the "solid" table that has generated dated rows.
select str_to_date('2010-01-01', '%Y-%m-%d') into #ad;
select * from
(select (#ad := date_add(#ad, INTERVAL 15 MINUTE)) as solid_date from wp_posts limit 95) solid
left join
wp_posts
on solid.solid_date = post_date
I've no idea how to generate an arbitrary number of rows in mysql so i'm just selecting from a table with more than 95 rows (24 hours * 4 appointments per hour less one at midnight) -- my wordpress posts table. Nothing stopping you making just such a table and having a single column with a single incrementing integer in if there are no better ways to do it (i'm an oracle guru not a mysql one). Maybe there isn't one: How do I make a row generator in MySQL?
Where you see wp_posts, substitute the name of your appointments table. Where you see the date, substitute your start date.
The query above produces a list of dates starting 15 after midnight on the chosen day (in my example 2010-01-01)
You can add a WHERE appointments.primary_key_column_here IS NULL if you want to find free slots only
Note you didn't include midnight in your spec. If you want midnight on the first day, start the date in the variable 15 minutes before and limit yourself to 96 rows. If you want midnight on the end day, just limit yourself to 96 rows