Average time between sql edits in php - php

I currently have a system to track inventory items.
The sql table is set up as follows:
Unique ID | Order number | Location | TimeStamp
Every time an order moves, a new entry is created with the same order number with the new location and timestamp.
Now I need to find the average time required for order to move from one location to another, say from Location Warehouse to Pickup Depot.
I am trying to work on the query and I have this so far.
SELECT
IFNULL(TIMESTAMPDIFF(SECOND,
MIN(TimeStamp),
MAX(TimeStamp)) / NULLIF(COUNT(*) - 1, 0), 0)
FROM TableName
WHERE Status = 'Delivered'
AND TimeStamp > DATE_SUB(NOW(), INTERVAL 6 HOUR)
The works really well if the table only had one order number, the moment we add more table numbers the average goes off.
I need it to only look at the timestamp difference for each order number, while currently I think its looking at the whole table.
Any help would be much appreciated.
Apologize for posting this question twice, the previous post did not contain enough information.
Thanks again.
SELECT
IFNULL(TIMESTAMPDIFF(SECOND,
MIN(TimeStamp),
MAX(TimeStamp)) / NULLIF(COUNT(*) - 1, 0), 0)
FROM TableName
WHERE Status = 'Delivered'
AND TimeStamp > DATE_SUB(NOW(), INTERVAL 6 HOUR)
GROUP BY OrderNumber
The above query returns the timedifference in different rows in sql (with the following error " Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.". The table has one column named "IFNULL(TIMESTAMPDIFF(SECOND, MIN(TimeStamp), MAX(TimeStamp)) / NULLIF(COUNT(*) - 1, 0), 0)" with the time difference for various orders arrange in rows. Now I am trying to get their average with the output code.
Am outputting the results with the following code:
$row_cnt = $result2->num_rows;
while ($row2 = mysqli_fetch_assoc($result2)) {
$processingseconds = $row2['IFNULL(TIMESTAMPDIFF(SECOND, MIN(TimeStamp), MAX(TimeStamp)) / NULLIF(COUNT(*) - 1, 0), 0)'] + $processingseconds;
}
print "Current Processing Time: ";
$processingseconds = $processingseconds/$row_cnt;
$processingminutes = $processingseconds/60;
echo $processingminutes;

Try adding a Group by condition to your query.
GROUP BY Order_Number_column
Something like this:
SELECT
IFNULL(TIMESTAMPDIFF(SECOND,
MIN(TimeStamp),
MAX(TimeStamp)) / NULLIF(COUNT(*) - 1, 0), 0)
FROM TableName
WHERE Status = 'Delivered'
AND TimeStamp > DATE_SUB(NOW(), INTERVAL 6 HOUR)
GROUP BY Your_Order_Number_column

Related

Time Record Calculations in PHP

I have a database table full of time records and I need to calculate the quantity of hours that exist between them...
A time record has the following fields: 'created' (i.e. 2017:08:30 11:15:00) and 'direction' (i.e. 1 represents "clock in" and "0" represents "clock out"). So I need to set a start and end date, then select all time records within that time frame and calculate the quantity of hours worked (the quantity of hours that exist between the records where direction=0 and direction=1).
Any idea how to create the logic for this? The result must be a measurement of "hours" in decimal format (1 decimal place, i.e. '26.7' hours).
I started by establishing variables:
$query_start_date = "2017-08-29 00:00:00";
$query_end_date = "2017-08-30 23:59:59";
Let's assume these are the time records that exist in that time frame:
time record 1: 'created'="2017-08-29 08:00:00", 'direction'=1;
time record 2: 'created'="2017-08-29 16:30:00", 'direction'=0;
time record 3: 'created'="2017-08-30 08:00:00", 'direction'=1;
time record 4: 'created'="2017-08-30 16:00:00", 'direction'=0;
But I don't know how to begin the calculation. Do I select the records first and assign each record to a variable as an array...? Any help is appreciated!
Make start and end time in two columns:
SELECT
created as start,
(select created from test t2 where t2.created > t.created
and direction = 1 order by created limit 1) as end
FROM `test` t where direction = 0
conunt time difference:
select TIME_TO_SEC(TIMEDIFF(end, start))/60/60 as diff, start, end from
(SELECT
created as start,
(select created from test t2 where
t2.created > t.created
and direction = 1 order by created limit 1) as end
FROM `test` t where direction = 0) intervals
and finally count sum:
select sum(TIME_TO_SEC(TIMEDIFF(end, start))/60/60) as total from
(SELECT
created as start,
(select created from test t2 where
t2.created > t.created
and direction = 1 order by created limit 1) as end
FROM `test` t where direction = 0) intervals

MySQL get records by quarters

Have problem to returning results from MySQL by quarters.
Mysql:
id, start_datetime, end_datetime and more columns.
I need to return records by start_datetime (for example it's - 2016-03-05 20:12:22 | MySQL column format is DateTime). How can I write MySQL query to display that record in a specific quarter?
I was searching a lot but didn't find anything that would work fo me: I tested this:
SELECT * FROM appointments
WHERE id_user = 80
AND DATE_SUB(start_datetime, INTERVAL 1 QUARTER)
AND hash = 'completed'
But it's not working correct.
This will give you the results for quarter 1:
SELECT * FROM appointments
WHERE id_user = 80
AND QUARTER(start_datetime) = 1
AND hash = 'completed'
Change the "1" to 2, 3, or 4 to get the other quarters
Here is the way to get all the data based on quarter inMySql or WordPress.
Note : visited is the datetime column into the table
QUARTER(your date) will give you the current quarter.
Previous Quarter
SELECT * FROM tbl_users WHERE QUARTER(visited) = QUARTER(curdate() - INTERVAL 1 quarter)
Current Quarter
SELECT * FROM tbl_users WHERE QUARTER(visited) = QUARTER(curdate())

MySQL Query to Determine Overlapping Timestamps - Schedule System

I have a table that stores shift records for employees.
Simply, there's the following data:
id = Shift ID
employeenum = Employee Number
start = unix timestamp of shift start time
end = unix timestamp of shift end time
date = YYYY-mm-dd description of date the shift starts on
status = shift status (numeric status identifier)
I am currently determining conflicts through a looping php script but it's far too slow. I've searched other questions and can't quite find the answer I'm looking for.
I am trying to come up with a query that will basically give me a list of employeenums that have conflicting shifts within a given time period.
i.e. for the period 2016-07-03 to 2016-07-10, which employees have overlapping start and end timestamps for shifts with a status value of 1 or 7.
Any help would be appreciated.
Thank you!
EDIT
This is essentially the table structure.
id is a primary auto increment key. The table is full of numeric data.
ID is an autoincremented number, employeenum is a 6 digit number, start and end are unix timetamps, date is YYYY-mm-dd date format, overridden is 1 or 0, status is 1,2,3,4,5,6, or 7.
Current loop works by querying:
SELECT * FROM schedule WHERE overridden =0 AND date >=$startdate AND date <= $enddate AND (status = 1 OR status = 7) AND employeenum != 0 ORDER BY date ASC
It then loops through all of those returned shifts to test whether or not another one conflicts with them by executing this query over and over (using the returned start and end values from the results of the above query):
SELECT `employeenum` FROM `schedule` WHERE `overridden` =0 AND `date` >= '$startdate' AND `date` <= '$enddate' AND (`status` = '1' OR `status` = '7') AND ((('$start' > `start`) AND ('$start' < `end`)) OR ((`end` > '$start') AND (`end` < '$end'))) AND `employeenum` = '$employee';"
If there is a result, it pushes the employee number to an array of employees with conflicts. This then prevents the loop from checking for that employee again.
At any given time there could be 10,000 records, so it's executing 10,000+ queries. These records represent only 100-200 employees, so I am looking for a way to query one time to see if there are any overlapping (start and end overlap with another start or end) records between two date values for one employeenum without having to query the database 10,000 times.
This Query will give you the shift id, the date, the employee number, the conflicting employee numbers and a count of conflicting shifts. You have a ton of shifts that conflict in your dataset!!!
SELECT `schedule_test`.`id`, `schedule_test`.`date`, `schedule_test`.`employeenum`, GROUP_CONCAT(DISTINCT`join_tbl`.`employeenum`), COUNT(`join_tbl`.`employeenum`)
FROM `schedule_test`
INNER JOIN `schedule_test` AS `join_tbl` ON
`schedule_test`.`date` = `join_tbl`.`date`
AND (`join_tbl`.`status` = 1 OR `join_tbl`.`status` = 7)
AND (`join_tbl`.`start` BETWEEN `schedule_test`.`start` AND `schedule_test`.`end`
OR `join_tbl`.`end` BETWEEN `schedule_test`.`start` AND `schedule_test`.`end`)
AND `schedule_test`.`id` != `join_tbl`.`id`
WHERE (`schedule_test`.`status` = 1 OR `schedule_test`.`status` = 7)
GROUP BY `schedule_test`.`id`
ORDER BY `schedule_test`.`date`
Adapted from #cmorrissey 's answer. THANK YOU!!
SELECT `schedule_test`.`id`, `schedule_test`.`date`,
`schedule_test`.`employeenum`,
GROUP_CONCAT(DISTINCT`join_tbl`.`employeenum`),
COUNT(`join_tbl`.`employeenum`)
FROM `schedule_test`
INNER JOIN `schedule_test` AS `join_tbl` ON
`schedule_test`.`date` = `join_tbl`.`date`
AND (`join_tbl`.`status` = 1 OR `join_tbl`.`status` = 7)
AND (`join_tbl`.`employeenum` = `schedule_test`.`employeenum`)
AND (`join_tbl`.`start` BETWEEN `schedule_test`.`start` AND `schedule_test`.`end`
OR `join_tbl`.`end` BETWEEN `schedule_test`.`start` AND `schedule_test`.`end`)
AND `schedule_test`.`id` != `join_tbl`.`id`
WHERE (`schedule_test`.`status` = 1 OR `schedule_test`.`status` = 7)
GROUP BY `schedule_test`.`id`
ORDER BY `schedule_test`.`date`

MYSQL & PHP calculate total hours in specific date and exclude overlaps hours

I have a MYSQL table for tasks where each task has a date, start time,end time and user_id. I want to calculate total number of hours on specific date.
Table Structure
CREATE TABLE tasks
(`id` int,`user_id` int, `title` varchar(30), `task_date` datetime, `start` time, `end` time)
;
INSERT INTO tasks
(`id`,`user_id`, `title`,`task_date`, `start`, `end`)
VALUES
(1,10, 'Task one','2013-04-02', '02:00:00', '04:00:00'),
(2,10, 'Task two','2013-04-02', '03:00:00', '06:00:00'),
(3,10, 'Task three.','2013-04-02','06:00:00', '07:00:00');
MYSQL Query
select TIME_FORMAT(SEC_TO_TIME(sum(TIME_TO_SEC(TIMEDIFF( end, start)))), "%h:%i") AS diff
FROM tasks
where task_date="2013-04-02"
The result am getting is "06:00" Hours which is fine, but I want to exclude the overlap hours. In the example I gave result should be "05:00" Hours when the hour between 3-4 in the 2nd record is excluded because this hour is already exist in the 1st record between 2-4.
1st record 2=>4 = 2 Hours
2nd record 3=>6 = 3 hours 3-1 hour=2 (The 1 hour is the overlap hour between 1st and 2nd record )
3rd 6=>7=1
Total is 5 Hours
I hope I made my question clear. Example http://sqlfiddle.com/#!2/05dd8/2
Here is an idea that uses variables (in other databases, CTEs and window functions would make this much easier). The idea is to first list all the times -- starts and ends. Then, keep track of the cumulative number of starts and stops.
When the cumulative number is greater than 0, then include the difference from the previous time. If equal to 0, then it is the beginning of a new time period, so nothing is added.
Here is an example of the query, which is simplified a bit for your data by not keeping track of changes in user_id:
select user_id, TIME_FORMAT(SEC_TO_TIME(sum(secs)), '%h:%i')
from (select t.*,
#time := if(#sum = 0, 0, TIME_TO_SEC(TIMEDIFF(start, #prevtime))) as secs,
#prevtime := start,
#sum := #sum + isstart
from ((select user_id, start, 1 as isstart
from tasks t
) union all
(select user_id, end, -1
from tasks t
)
) t cross join
(select #sum := 0, #time := 0, #prevtime := 0) vars
order by 1, 2
) t
group by user_id;
Here is a SQL Fiddle showing it working.

try to get current and next schedule by php

i make a php program
this programe show Schedules of channels
in the page of the channel i want to display the current program and the next one ,
in the table of the Schedules i insert the time of the program in cell called " when "
select when from Schedules where channel_id = 13
this is will display the time of all the programms in timestamp
i wanna to output only the current program , and the next one only
sorry for bad english language
I don't have something to test this against, however this query will get all schedules for channel id 13 that are scheduled after or at $time, we order all such results in asc order by when and finally choose first two out of them (which will be the program at $time and the next after this one)
SELECT field1,field2,when FROM Schedules where channel_id = 13 AND
when >= $time ORDER BY when ASC LIMIT 2
Fill in for field1, field2, and $time variable as required.
Assuming when column is of DATETIME type:
( SELECT * FROM Schedules where channel_id = 13 AND when <= NOW() ORDER BY when DESC LIMIT 1 )
UNION
( SELECT * FROM Schedules where channel_id = 13 AND when > NOW() ORDER BY when ASC LIMIT 1 )

Categories