Get time difference for each row in table - mysql

I am working on an online time card system and have ran into a road block. What I need to do is calculate in each row the timeIn and timeOut for a date the employee might clock in and out several times a day. I have a working query to find the total for the entire day but not for each individual row. I have included the structure of the database table with some examples of what I would like to see.
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
| id | employeeID | date | timeIn | jobDescription | equipType | unitNumber | unitHours | timeOut |
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
| 8 | 1 | 01/15/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 9:30 PM |
| 9 | 1 | 01/15/13 | 10:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 12:00 PM |
| 10 | 1 | 01/16/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 5:30 PM |
| 11 | 1 | 01/17/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 5:30 PM |
| 12 | 1 | 01/18/13 | 8:00 AM | Backfill in Whispering Creek | Skid Loader | 297 | NULL | 5:30 PM |
| 13 | 1 | 01/19/13 | 8:00 AM | Backfill in Whispering Creek | Skid Loader | 297 | NULL | 3:30 PM |
| 1 | 1 | 01/20/13 | 6:00 | | Excavator | 01E | 7238 | 17:00 |
| 2 | 1 | 01/21/13 | 6:00 | Worked in Jefferson | Excavator | 01E | 7238 | 17:00 |
| 3 | 1 | 01/22/13 | 6:00 | Worked in Jefferson | Excavator | 02E | 7238 | 17:30 |
| 4 | 1 | 01/23/13 | 6:00 | Worked in Whispering Creek | Skid Loader | 32SL | 2338 | 18:30 |
| 5 | 1 | 01/24/13 | 8:00 | Worked in Hubbard | Scraper | 54C | 9638 | 11:30 |
| 6 | 1 | 01/25/13 | 8:00 | Worked in Jefferson | Dozer | 4D | 941 | 19:30 |
| 7 | 1 | 01/26/13 | 8:00 | Pushed Snow | Loader | 950H | 342 | 20:30 |
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
Ok so what I would like to see is that for example the date 01/15/13 appears twice with different timeIn and timeOut values I would like to get a value back saying that in row with the id 8 there was 2:30 hours, then get another value back saying that in the row where id is 9 there was 2:00 hours. I would like to accomplish this with one query if possible.
This is my query to get the time for the entire day:
SELECT `employeeID`, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`timeOut`, `timeIn`)))) AS `totalTime` FROM `timeRecords` WHERE `date` = "01/15/13" AND `employeeID` = 1 GROUP BY `employeeID`;

If I understand you correctly you need this?
SELECT `employeeID`, TIMEDIFF(`timeOut`, `timeIn`) AS `totalTime` FROM `timeRecords`;
You can use PDO query to query your database which returns result set as array of associative array. For better performance you can use PDO prepare/execute.
<?php
function getTimeByRow($connection) {
$sql = "SELECT `employeeID`, TIMEDIFF(`timeOut`, `timeIn`) AS `totalTime` FROM `timeRecords`";
foreach ($connection->query($sql) as $row) {
print $row['employeeID'] . "\t";
print $row['totalTime'] . "\n";
}
}
?>

So you want to do something like this:
select employeeID, date, TIMEDIFF(timeout, timein) from timeRecords where date = x and employeeID = y

Related

How to update status of current fiscal year and add new fiscal year using MySQL event scheduler?

I have a table name tbl_fy, I want that status should change automatically from Y to N by the given fy_end_date: 6/30/2021 and meanwhile it also add new fiscal year record in new rows from 7/1/2021 to 6/30/2022.Please help me senior developer in this regard and provide me MySQL event scheduler code in MySQL I will be grateful to you.
+===========+===============+=============+=========+===============+=================+=================+
| fy_doc_id | fy_start_date | fy_end_date | fy_year | fy_active_sts | maker_datetime | update_datetime |
+===========+===============+=============+=========+===============+=================+=================+
| 1 | 7/1/2018 | 6/30/2019 | 2018-19 | N | 2/22/2021 11:07 | 6/2/2021 12:02 |
+-----------+---------------+-------------+---------+---------------+-----------------+-----------------+
| 2 | 7/1/2019 | 6/30/2020 | 2019-20 | N | 8/19/2020 4:10 | 6/2/2021 12:02 |
+-----------+---------------+-------------+---------+---------------+-----------------+-----------------+
| 3 | 7/1/2020 | 6/30/2021 | 2020-21 | Y | 8/19/2020 4:10 | |
+-----------+---------------+-------------+---------+---------------+-----------------+-----------------+

Conditionally move MySQL data between rows in same table

Working in Redmine, I need to copy(not move) data from certain rows to other rows based on matching project id numbers with time entries.
I have included a diagram of the table "custom_values" and my understanding of the design below(CURRENT DATA):
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | |
+----+-----------------+---------------+-----------------+-------+
At the risk of oversimplifying,
"id" = The primary key for each entry in custom_values
"customized_type" = Specifies which db table the row is referring to.
"customized_id" = Specifies the primary key for the db table entry previously specified in "customized_type".
"custom_field_id" = Specifies which custom field the row is referring to. Redmine admins can arbitrarily add and remove custom fields.
"value" = The data contained within the custom field specified by
"custom_field_id"
In my situation, the values listed in "value" are representing unique customer id numbers. The customer id numbers did not always get entered with each time entry. I need to copy the customer numbers from the project rows to the matching time entry rows. Each time entry has a project_id field.
So far, here is my mangled SQL query:
SELECT
custom_field_id,
custom_values.value AS 'CUSTOMER_NUMBER',
custom_values.customized_id AS 'PROJECT_ID_NUMBER',
custom_values.customized_type,
time_entries.comments AS 'TIME_ENTRY_COMMENTS'
FROM
redmine_tweaking.custom_values
LEFT JOIN
redmine_tweaking.time_entries ON custom_values.customized_id = time_entries.project_id
WHERE
custom_values.customized_type='Project' AND custom_values.custom_field_id=1;
The query I have so far allows me to see that I have the time entries connected properly to their matching projects, but that is all I have been able to figure out. So in other words, this SQL statement does not exactly solve my problem.
Plus, even if it did work, I think the way I laid it out looks like 200 lbs of bird poop. There must be a better/more optimized way to do this.
Any help would be greatly appreciated. I am relatively new and I have been pouring hours into solving this problem.
UPDATE:
Ok, here is the time_entries table:
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| id | project_id | user_id | issue_id | hours | comments | activity_id | spent_on | tyear | tmonth | tweek | created_on | updated_on |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | .25 | test | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 08:18:12 | 2015-11-04 10:18:12 |
| 2 | 2 | 1 | 1 | .25 | test2 | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 09:18:12 | 2015-11-04 12:18:12 |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
As opposed to the original table that I first posted, the expected output would show this:
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | 03 |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | 04 |
+----+-----------------+---------------+-----------------+-------+

Selection in mysql with hours available like right outer join

I have two tables in my database. I use MySQL.
Basically, I created an app to manage 'Futsal's Field Order'
So, here we go :
The first table is named Lapangan means "Field in Indonesian" :
mysql> SELECT id,nama_lapangan FROM lapangan;
+----+---------------+
| id | nama_lapangan |
+----+---------------+
| 1 | Lap 01 |
| 2 | Lap 02 |
| 3 | Lap 03 |
+----+---------------+
3 rows in set (0.00 sec)
And The Second Table is Booking : ,
mysql> SELECT id, nomor_booking, date_booking, date_end_booking, lapangan_id FROM `yfutsal`.`booking` LIMIT 1000;
+----+---------------+---------------------+---------------------+-------------+
| id | nomor_booking | date_booking | date_end_booking | lapangan_id |
+----+---------------+---------------------+---------------------+-------------+
| 1 | 1 | 2017-07-16 10:00:00 | 2017-07-16 12:00:00 | 1 |
| 2 | 2 | 2017-07-16 15:00:00 | 2017-07-16 16:00:00 | 3 |
+----+---------------+---------------------+---------------------+-------------+
For example, The user start at 08.00 AND end in 23.00.
It means, lap 1 is not available on 10.00 - 12.00.
And same too that lap 3 is not available on 15.00 - 16.00.
The goal is, I want to display the Lapangan (field) that available with hour so, the cashier can choice it.
Something like this :
+----+---------------+----------------------+-----------------------+
| id | nama_lapangan | Available Start | Available End |
+----+---------------+----------------------+-----------------------+
| 1 | Lap 01 | 2017-07-16 08:00:00 | 2017-07-16 09:59:00 |
| 1 | Lap 01 | 2017-07-16 12:01:00 | 2017-07-16 23:00:00 |
| 2 | Lap 02 | 2017-07-16 08:00:00 | 2017-07-16 23:00:00 |
| 3 | Lap 03 | 2017-07-16 08:00:00 | 2017-07-16 14:59:00 |
| 3 | Lap 03 | 2017-07-16 16:01:00 | 2017-07-16 23:00:00 |
+----+---------------+----------------------+-----------------------+
Please advise.
just change
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
in config/config to
$root = "https://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
base_url() function returns value based on base_url constant , you just have to make the constant return https instead of http
try this to set base_url in proper way:
$root=(isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

Displaying records from database by quarter in mysql

I have a table named nca_totals.
Table: nca_totals
+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date | account_type |
+----------+-----------+------------+--------------+
| 13 | 10450 | 2015-01-21 | DBP-TRUST |
| 14 | 5000 | 2015-02-05 | DBP-TRUST |
| 15 | 7000 | 2015-04-02 | DBP-TRUST |
| 16 | 4000 | 2015-05-02 | DBP-TRUST |
+----------+-----------+------------+--------------+
Now I want to display all the data by quarter base on its date. Let's say I want to display all the records who belong to 1st Quarter like this:
+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date | account_type |
+----------+-----------+------------+--------------+
| 13 | 10450 | 2015-01-21 | DBP-TRUST |
| 14 | 5000 | 2015-02-05 | DBP-TRUST |
+----------+-----------+------------+--------------+
This date belongs to the 1st quarter of the year (Jan, Feb, March). I only have this query to select the date and return its quarter number as:
SELECT QUARTER('2015-01-11'); /* returns 1 */
How can I combine that query to display all the records by quarter ? Can anyone help ? Thanks.
select *
from nca_totals
where QUARTER(nca_date) = 1
SELECT
CEIL(MONTH(`nca_date`) / 3) AS `quarter`
FROM `nca_totals`;

Getting the missing period through mmysql query or procedure..?

I have a table customer_order as follows
mysql> select * from customer_order;
+---------+---------+-----------+------------------+----------------+
| cust_id | orderno | region_cd | order_start_date |order_del_date |
+---------+---------+-----------+------------------+----------------+
| CU_082 | ONO_001 | reg1 | 2012-04-25 | 2012-08-25 |
| CU_082 | ONO_002 | reg1 | 2012-04-28 | 2012-11-28 |
| CU_083 | ONO_002 | reg2 | 2012-04-28 | 2012-11-28 |
| CU_082 | ONO_003 | reg1 | 2012-04-25 | 2012-08-25 |
| CU_084 | ONO_004 | reg4 | 2012-04-25 | 2012-10-25 |
I need a table like this...which i get....as follows
mysql> select order_start_date,order_del_date,orderno,cust_id from customer_order wh
ere order_start_date >= '2012-04-25' AND order_del_date <='2012-12-28' and cust_i
d IN ('36082','36088') order by cust_id ;
+------------------+----------------+---------+---------+
| order_start_date | order_del_date | pid | emp_id |
+------------------+----------------+---------+---------+
| 2012-04-25 | 2012-05-25 | ONO_001 | CU_082 |
| 2012-08-22 | 2012-12-28 | ONO_004 | CU_082 |
| 2012-06-22 | 2012-08-28 | ONO_003 | CU_082 |
| 2012-05-27 | 2012-06-25 | ONO_002 | CU_082 |
| 2012-04-25 | 2012-05-25 | ONO_001 | CU_082 |
| 2012-05-27 | 2012-06-25 | ONO_001 | CU_082 |
| 2012-04-30 | 2012-06-25 | ONO_001 | CU_088 |
| 2012-06-28 | 2012-07-15 | ONO_002 | CU_088 |
| 2012-07-28 | 2012-08-25 | ONO_003 | CU_088 |
| 2012-07-16 | 2012-09-25 | ONO_004 | CU_088 |
+------------------+----------------+---------+---------+
now i need to query on this table ...
to get
for each customer here we get data for the period from wat date to wat date his order processing details in the above table..
now for each customer i shld find the period for which thr is no order processing...
eg cust_id =CU_088
he has his order processed from 30 apr to 25 june
den from 28th june to 15 july
(here thr is a diff that is thr is no order taken or any processing done from 26th to 27th june..this is wat is the required result)
**one more imp consideration is...
in the next entry we find thr is a order process from 28th july to 25th aug
w.r.t previous entry i.e, 28th june to 15july we find that for this customer thr is no order taken or processed from 16th to 27th july..
but with the last entry tat is 16th july to 25th sept he has an order with different order_no thrfore the gap 16th july to 27th july is filled here so this kind of a condition also needs to be checked...
I need to get the output as something like this..
+------------------+----------------+---------+---------+
| order_start_date | order_del_date | pid | emp_id |
+------------------+----------------+---------+---------+
| 2012-06-26 | 2012-06-27 | ONO_001 | CU_088 |
+------------------+----------------+---------+---------+
that is either the query or procedure which is more efficient should give me the period wer in thr was no action done for customer...
help me write the query which does the all the above things.
I m new to db queries..so please help me out..
To fetch every 'gap' between orders, you can use a self-join:
SELECT o1.cust_id,
o1.order_del_date + INTERVAL 1 DAY AS gap_begin,
MIN(o2.order_start_date) - INTERVAL 1 DAY AS gap_end
FROM customer_order o1
JOIN customer_order o2 ON o1.cust_id = o2.cust_id
AND o1.order_del_date <= o2.order_start_date
GROUP BY o1.cust_id, o1.order_del_date
HAVING gap_begin < gap_end