How to put empty set in inner join - mysql

I am a complete beginner in MySql and I wanted you to help me. I want a DTR User Interface where there is time in and time out. So I have a fields like -id_biometrics,empno,datecreated,time_created,status,device the problem is if the employee forgot to time in or time out, I want to put the time in or time out to "NULL" here is what I coded.
SELECT start_log.empno AS "Employee Number", start_log.date_created, start_log.time_created AS "Time In", end_log.time_created AS "Time Out"
FROM biometrics AS start_log
INNER JOIN biometrics AS end_log ON start_log.empno = end_log.empno
WHERE start_log.status =0
AND end_log.status =1
AND start_log.empno =2
GROUP BY start_log.date_created, start_log.empno
And what my output here if he completes his time in and time out for the day is.
Employee Number|date_created |Time_in | Time_Out
2 | 2013-07-15 | 11:08:07 | 15:00:00
And if he/she forgots to Time Out or time in it shows MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0016 sec )
What I wanted is
Employee Number | date_created |Time_in | Time_Out
3 | 2013-07-15 | 11:50:00 | Null
Please help me with this. I needed it badly thanks.

Are you looking for this?
SELECT empno, date_created,
MIN(CASE WHEN status = 0 THEN time_created END) time_in,
MIN(CASE WHEN status = 1 THEN time_created END) time_out
FROM biometrics
GROUP BY empno, date_created
Sample output:
+-------+--------------+----------+----------+
| empno | date_created | time_in | time_out |
+-------+--------------+----------+----------+
| 2 | 2013-07-15 | 11:08:07 | 15:00:00 |
| 3 | 2013-07-15 | 11:50:00 | NULL |
| 4 | 2013-07-15 | NULL | 16:00:00 |
+-------+--------------+----------+----------+
Here is SQLFiddle demo
UPDATE If you may have multiple in or out records and you always want to fetch first in and last out then just use MAX() aggregate for time_out
SELECT empno, date_created,
MIN(CASE WHEN status = 0 THEN time_created END) time_in,
MAX(CASE WHEN status = 1 THEN time_created END) time_out
FROM biometrics
GROUP BY empno, date_created
Here is SQLFiddle demo

Related

How do I aggregate from multiple rows

I have the below table:
Person_ID | Timestamp | Action
1 | 01-01-2020 00:01:00 | LOGGED ON
1 | 01-01-2020 00:02:00 | ON BREAK
1 | 01-01-2020 00:02:30 | OFF BREAK
1 | 01-01-2020 00:04:00 | LOGGED OFF
1 | 01-01-2020 00:04:30 | LOGGED ON
1 | 01-01-2020 00:05:30 | ON BREAK
1 | 01-01-2020 00:06:00 | OFF BREAK
1 | 01-01-2020 00:08:00 | LOGGED OFF
2 | 01-02-2020 00:04:00 | LOGGED ON
2 | 01-02-2020 00:10:00 | LOGGED OFF
My goal is to write an SQL view to get the below result:
Person_ID | LoggedInTime_hours | OnBreakTime_hours
1 | 6.5 | 1
2 | 6 | 0
For each Person_ID, calculate the difference in hours between each pair of occurrences of LOGGED IN/LOGGED OUT and ON BREAK/OFF BREAK respectively.
Thanks a lot for your expertise and help.
1st of all LoggedInTime_hours for PERSON_ID = 1 should be 5.5 hours not 6.5 hours.
Let us assume the table name is TEST.
Database : MySQL
SELECT
PERSON_ID,
IFNULL(SUM(CASE WHEN PREV_ACTIVE_IND - ACTIVE_IND > 0 THEN TIMESTAMPDIFF(SECOND, PREV_TIMESTAMP, TIMESTAMP)/3600 END),0) LoggedInTime_hours,
IFNULL(SUM(CASE WHEN PREV_ACTIVE_IND - ACTIVE_IND = 0 THEN TIMESTAMPDIFF(SECOND, PREV_TIMESTAMP, TIMESTAMP)/3600 END),0) OnBreakTime_hours
FROM
(SELECT
PERSON_ID,
PREV_ACTION,
ACTION,
CASE WHEN PREV_ACTION = 'LOGGED OFF' THEN 1
WHEN PREV_ACTION LIKE '%BREAK' THEN 2
WHEN PREV_ACTION IS NULL THEN NULL
ELSE 3
END AS PREV_ACTIVE_IND,
CASE WHEN ACTION = 'LOGGED OFF' THEN 1
WHEN ACTION LIKE '%BREAK' THEN 2
WHEN ACTION IS NULL THEN NULL
ELSE 3
END AS ACTIVE_IND,
PREV_TIMESTAMP,
TIMESTAMP
FROM
(SELECT PERSON_ID,
TIMESTAMP,
LAG(TIMESTAMP,1) OVER (PARTITION BY PERSON_ID) PREV_TIMESTAMP,
LAG(ACTION,1) OVER (PARTITION BY PERSON_ID) PREV_ACTION,
ACTION
FROM TEST) T) T2
GROUP BY PERSON_ID
Hope this query will help.
Try the following query using MySQL v8.0:
With CTE As
(
Select Person_ID, Action_,
TIME_TO_SEC(
TIMEDIFF(
Lead(Timestamp_) Over (Partition By Person_ID
Order By Case When Action_ In 'LOGGED ON','LOGGED OFF')
Then 1 Else 2 End,Timestamp_), Timestamp_
))/3600 As DIFF
From MyData
)
Select Person_ID,
Sum(Case When Action_='LOGGED ON' Then DIFF Else 0 End) As LoggedInTime_hours,
Sum(Case When Action_='ON BREAK' Then DIFF Else 0 End) As OnBreakTime_hours
From CTE
Group By Person_ID
See a demo from db-fiddle.
The Lead() function will find the next action date related to the current action i.e. (Logged In -> Logged Off), the actions are ordered according to their types; ('LOGGED ON','LOGGED OFF') ordered with 1, and ('ON BREAK','OFF BREAK') ordered with 2, then ordered by Timestamp of the action. Doing so, will ensure that each 'LOGGED ON' is followed by it's 'LOGGED OFF' action, and each 'ON BREAK' is followed by it's 'OFF BREAK' action.
Using TIMEDIFF we can find the difference between the actions times for each pair of actions ('LOGGED ON','LOGGED OFF'), ('ON BREAK','OFF BREAK'). Then the sum of these differences will be LoggedInTime_hours for logged in actions, and OnBreakTime_hours for on break actions.
The TIME_TO_SEC()/3600 is used to convert time differences to hours.

MYSQL - Finding row delta using date rows that include holidays

I have a table that includes a field with dates (call it date) and a field with a cumulative running total (call it X) | call it table SAMPLE.
***My data in field DATE does not include weekends and holidays.
I can find the delta in the numbers from day to day by simply subtracting any chosen value in "X" and subtracting that from the row above.
Here's my current query:
select
date,
a.X - b.X as 'Daily Total'
from SAMPLE as a
left join SAMPLE as b
on b.date = if(weekday(a.date) = 0 , a.date - interval 3 day, a.date- interval 1 day);
The problem is that the above values work until I hit dates with holidays. If Monday is a holiday, then the values return null because a.date - interval 1 day will not exist. What's the best way to go about solving the holidays issue?
the below are the current results:
+------------+---------------+
| date | X |
+------------+---------------+
| 2018-03-26 | -40105.00 |
| 2018-03-27 | 28470.00 |
| 2018-03-28 | 5265.00 |
| 2018-03-29 | -23010.00 |
| 2018-04-02 | NULL |
| 2018-04-03 | -24830.00 |
| 2018-04-04 | -21970.00 |
| 2018-04-05 | -9620.00 |
| 2018-04-06 | 36465.00 |
Thanks in advance!!
I will sort the table by date then assign a sequence or series of numbers from 1 to n. I will then subtract the value of current row from the previous row except the first row. For first row, i will copy the value X.
select rnk2.`date`,
case when rnk1.r1=1 and rnk2.r2=1 then rnk1.X else rnk2.X-rnk1.X end as 'Daily Total'
from (
select `date`,X,#r1:=#r1+1 as r1
from samples, (select #r1:=0) a
order by `date` ) rnk1
inner join
(select `date`,X,#r2:=#r2+1 as r2
from samples, (select #r2:=0) b
order by `date`) rnk2
on (rnk1.r1=1 and rnk2.r2=1) or (rnk1.r1+1=rnk2.r2)
order by rnk2.`date`

Finding total active hours by calculating difference between TimeDate records

I have a table to register users logs every one minute and other activities using DateTime for each user_id
This is a sample data of my table
id | user_id | log_datetime
------------------------------------------
1 | 1 | 2016-09-25 13:01:08
2 | 1 | 2016-09-25 13:04:08
3 | 1 | 2016-09-25 13:07:08
4 | 1 | 2016-09-25 13:10:08
5 | 2 | 2016-09-25 13:11:08
6 | 1 | 2016-09-25 13:13:08
7 | 2 | 2016-09-25 13:13:09
8 | 2 | 2016-09-25 13:14:10
I would like to calculate the total active time on the system
UPDATE: Expected Output
For Example user_id 1 his total available time should be 00:12:00
Since his hours and seconds are same so I'll just subtract last log from previous then previous from next previous and so on then I'll sum all subtracted values
this a simple for
Simply I want to loop through the data from last record to first record with in my range
this is a simple formula I hope that make my question clear
SUM((T< n > - T< n-1 >) + (T< n-1 > - T< n-2 >) ... + (T< n-x > - T< n-first >))
Since user_id 1 his hours and seconds are the same then I'll calculate the minutes only.
(13-10)+(10-7)+(7-4)+(4-1) = 12
user_id | total_hours
---------------------------------
1 | 00:12:00
2 | 00:03:02
I did this code
SET #start_date = '2016-09-25';
SET #start_time = '13:00:00';
SET #end_date = '2016-09-25';
SET #end_time = '13:15:00';
SELECT
`ul1`.`user_id`, SEC_TO_TIME(SUM(TIME_TO_SEC(`dl1`.`log_datetime`))) AS total_hours
FROM
`users_logs` AS `ul1`
JOIN `users_logs` AS `ul2`
ON `ul1`.`id` = `ul2`.`id`
WHERE
`ul1`.`log_datetime` >= CONCAT(#start_date, ' ', #start_time)
AND
`ul2`.`log_datetime` <= CONCAT(#end_date, ' ', #end_time)
GROUP BY `ul1`.`user_id`
But this code Sum all Time not getting the difference. This is the output of the code
user_id | total_hours
---------------------------------
1 | 65:35:40
2 | 39:38:25
How can I calculate the Sum of all difference datetime, then I want to display his active hours every 12 hours (00:00:00 - 11:59:59) and (12:00:00 - 23:59:59) with in selected DateTime Period at the beginning of the code
So the output would look like this (just an dummy example not from given data)
user_id | total_hours | 00_12_am | 12_00_pm |
-------------------------------------------------------
1 | 10:10:40 | 02:05:20 | 08:05:20 |
2 | 04:10:20 | 01:05:10 | 03:05:30 |
Thank you
So you log every minute and if a user is available there is a log entry.
Then count the logs per user, so you have the number of total minutes.
select user_id, count(*) as total_minutes
from user_logs
group by user_id;
If you want them displayed as time use sec_to_time:
select user_id, sec_to_time(count(*) * 60) as total_hours
from user_logs
group by user_id;
As to conditional aggregation:
select
user_id,
count(*) as total_minutes,
count(case when hour(log_datetime) < 12 then 1 end) as total_minutes_am,
count(case when hour(log_datetime) >= 12 then 1 end) as total_minutes_pm
from user_logs
group by user_id;
UPDATE: In order to count each minute just once count distinct minutes, i.e. DATE_FORMAT(log_datetime, '%Y-%m-%d %H:%i'). This can be done with COUNT(DISTINCT ...) or with a subquery getting distinct values.
The complete query:
select
user_id,
count(*) as total_minutes,
count(case when log_hour < 12 then 1 end) as total_minutes_am,
count(case when log_hour >= 12 then 1 end) as total_minutes_pm
from
(
select distinct
user_id,
date_format(log_datetime, '%y-%m-%d %h:%i') as log_moment,
hour(log_datetime) as log_hour
from.user_logs
) log
group by user_id;

How merge two select with different WHERE and special conditions

I have table something like this:
date|status|value
date is date,
status is 1 for pending, 2 to confirmed
and value is value of order
I want to get 3 columns:
date|#status pending|#status pending+confirmed
example of data:
+------------+-----------------+-----------------+
| date | status | value |
+------------+-----------------+-----------------+
| 2015-11-17 | 1 | 89|
| 2015-11-16 | 1 | 6 |
| 2015-11-16 | 2 | 16 |
| 2015-11-16 | 2 | 26 |
| 2015-11-15 | 2 | 26 |
| 2015-11-14 | 2 | 24 |
+------------+-----------------+-----------------+
example of what I want:
+------------+-----------------+-----------------+
| date | confirmed |confirmed+pending|
+------------+-----------------+-----------------+
| 2015-11-17 | 0 | 1 |
| 2015-11-16 | 2 | 3 |
| 2015-11-15 | 1 | 1 |
| 2015-11-14 | 1 | 1 |
+------------+-----------------+-----------------+
I am trying to do:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(value) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5
) AS array1
INNER JOIN (
SELECT DATE (DATE) AS DATE
,count(value) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
) AS array2
But I get 4 results per date with repeated confirmed value and different total transactions.
If I try separated, I can get both correct informations:
will list only sum of confirmed orders of last 5 days:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(valor) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5;
)
will list sum of all orders of last 5 days:
SELECT DATE (DATE) AS DATE
,count(valor) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
I observed at least one big problem:
Sometimes we will have one day with a lot of not confirmed orders and zero confirmed, so probably inner join will fail.
You can use CASE WHEN, To get the expected output,you have given.
SELECT `date`,
(SUM(CASE WHEN `status`=1 THEN 1 ELSE 0 END)) AS Confirmed,
(SUM(CASE WHEN `status`=1 OR `status`=2 THEN 1 ELSE 0 END)) AS Confirmed_Pending
FROM
table_name
GROUP BY DATE(`date`) DESC
Hope this helps.
You are missing an ON clause in your INNER JOIN. Or, since in your case the column you join on is the same on both sides, you can use USING:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(value) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5
) AS array1
INNER JOIN (
SELECT DATE (DATE) AS DATE
,count(value) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
) AS array2
USING (DATE)
An easier approach could be to use a case expression to evaluate whether the status is something you'd like to count, and apply the count function to that:
SELECT DATE (`date`) AS `date`,
COUNT(CASE status WHEN 2 THEN 1 END) AS `confirmed`,
COUNT(CASE WHEN status IN (1, 2) THEN 1 END) AS `pending and confirmed`,
FROM orders
GROUP BY DATE (`date`) DESC

MySQL: group by consecutive days and count groups

I have a database table which holds each user's checkins in cities. I need to know how many days a user has been in a city, and then, how many visits a user has made to a city (a visit consists of consecutive days spent in a city).
So, consider I have the following table (simplified, containing only the DATETIMEs - same user and city):
datetime
-------------------
2011-06-30 12:11:46
2011-07-01 13:16:34
2011-07-01 15:22:45
2011-07-01 22:35:00
2011-07-02 13:45:12
2011-08-01 00:11:45
2011-08-05 17:14:34
2011-08-05 18:11:46
2011-08-06 20:22:12
The number of days this user has been to this city would be 6 (30.06, 01.07, 02.07, 01.08, 05.08, 06.08).
I thought of doing this using SELECT COUNT(id) FROM table GROUP BY DATE(datetime)
Then, for the number of visits this user has made to this city, the query should return 3 (30.06-02.07, 01.08, 05.08-06.08).
The problem is that I have no idea how shall I build this query.
Any help would be highly appreciated!
You can find the first day of each visit by finding checkins where there was no checkin the day before.
select count(distinct date(start_of_visit.datetime))
from checkin start_of_visit
left join checkin previous_day
on start_of_visit.user = previous_day.user
and start_of_visit.city = previous_day.city
and date(start_of_visit.datetime) - interval 1 day = date(previous_day.datetime)
where previous_day.id is null
There are several important parts to this query.
First, each checkin is joined to any checkin from the previous day. But since it's an outer join, if there was no checkin the previous day the right side of the join will have NULL results. The WHERE filtering happens after the join, so it keeps only those checkins from the left side where there are none from the right side. LEFT OUTER JOIN/WHERE IS NULL is really handy for finding where things aren't.
Then it counts distinct checkin dates to make sure it doesn't double-count if the user checked in multiple times on the first day of the visit. (I actually added that part on edit, when I spotted the possible error.)
Edit: I just re-read your proposed query for the first question. Your query would get you the number of checkins on a given date, instead of a count of dates. I think you want something like this instead:
select count(distinct date(datetime))
from checkin
where user='some user' and city='some city'
Try to apply this code to your task -
CREATE TABLE visits(
user_id INT(11) NOT NULL,
dt DATETIME DEFAULT NULL
);
INSERT INTO visits VALUES
(1, '2011-06-30 12:11:46'),
(1, '2011-07-01 13:16:34'),
(1, '2011-07-01 15:22:45'),
(1, '2011-07-01 22:35:00'),
(1, '2011-07-02 13:45:12'),
(1, '2011-08-01 00:11:45'),
(1, '2011-08-05 17:14:34'),
(1, '2011-08-05 18:11:46'),
(1, '2011-08-06 20:22:12'),
(2, '2011-08-30 16:13:34'),
(2, '2011-08-31 16:13:41');
SET #i = 0;
SET #last_dt = NULL;
SET #last_user = NULL;
SELECT v.user_id,
COUNT(DISTINCT(DATE(dt))) number_of_days,
MAX(days) number_of_visits
FROM
(SELECT user_id, dt
#i := IF(#last_user IS NULL OR #last_user <> user_id, 1, IF(#last_dt IS NULL OR (DATE(dt) - INTERVAL 1 DAY) > DATE(#last_dt), #i + 1, #i)) AS days,
#last_dt := DATE(dt),
#last_user := user_id
FROM
visits
ORDER BY
user_id, dt
) v
GROUP BY
v.user_id;
----------------
Output:
+---------+----------------+------------------+
| user_id | number_of_days | number_of_visits |
+---------+----------------+------------------+
| 1 | 6 | 3 |
| 2 | 2 | 1 |
+---------+----------------+------------------+
Explanation:
To understand how it works let's check the subquery, here it is.
SET #i = 0;
SET #last_dt = NULL;
SET #last_user = NULL;
SELECT user_id, dt,
#i := IF(#last_user IS NULL OR #last_user <> user_id, 1, IF(#last_dt IS NULL OR (DATE(dt) - INTERVAL 1 DAY) > DATE(#last_dt), #i + 1, #i)) AS
days,
#last_dt := DATE(dt) lt,
#last_user := user_id lu
FROM
visits
ORDER BY
user_id, dt;
As you see the query returns all rows and performs ranking for the number of visits. This is known ranking method based on variables, note that rows are ordered by user and date fields. This query calculates user visits, and outputs next data set where days column provides rank for the number of visits -
+---------+---------------------+------+------------+----+
| user_id | dt | days | lt | lu |
+---------+---------------------+------+------------+----+
| 1 | 2011-06-30 12:11:46 | 1 | 2011-06-30 | 1 |
| 1 | 2011-07-01 13:16:34 | 1 | 2011-07-01 | 1 |
| 1 | 2011-07-01 15:22:45 | 1 | 2011-07-01 | 1 |
| 1 | 2011-07-01 22:35:00 | 1 | 2011-07-01 | 1 |
| 1 | 2011-07-02 13:45:12 | 1 | 2011-07-02 | 1 |
| 1 | 2011-08-01 00:11:45 | 2 | 2011-08-01 | 1 |
| 1 | 2011-08-05 17:14:34 | 3 | 2011-08-05 | 1 |
| 1 | 2011-08-05 18:11:46 | 3 | 2011-08-05 | 1 |
| 1 | 2011-08-06 20:22:12 | 3 | 2011-08-06 | 1 |
| 2 | 2011-08-30 16:13:34 | 1 | 2011-08-30 | 2 |
| 2 | 2011-08-31 16:13:41 | 1 | 2011-08-31 | 2 |
+---------+---------------------+------+------------+----+
Then we group this data set by user and use aggregate functions:
'COUNT(DISTINCT(DATE(dt)))' - counts the number of days
'MAX(days)' - the number of visits, it is a maximum value for the days field from our subquery.
That is all;)
As data sample provided by Devart, the inner "PreQuery" works with sql variables. By defaulting the #LUser to a -1 (probable non-existent user ID), the IF() test checks for any difference between last user and current. As soon as a new user, it gets a value of 1... Additionally, if the last date is more than 1 day from the new date of check-in, it gets a value of 1. Then, the subsequent columns reset the #LUser and #LDate to the value of the incoming record just tested against for the next cycle. Then, the outer query just sums them up and counts them for the final correct results per the Devart data set of
User ID Distinct Visits Total Days
1 3 9
2 1 2
select PreQuery.User_ID,
sum( PreQuery.NextVisit ) as DistinctVisits,
count(*) as TotalDays
from
( select v.user_id,
if( #LUser <> v.User_ID OR #LDate < ( date( v.dt ) - Interval 1 day ), 1, 0 ) as NextVisit,
#LUser := v.user_id,
#LDate := date( v.dt )
from
Visits v,
( select #LUser := -1, #LDate := date(now()) ) AtVars
order by
v.user_id,
v.dt ) PreQuery
group by
PreQuery.User_ID
for a first sub-task:
select count(*)
from (
select TO_DAYS(p.d)
from p
group by TO_DAYS(p.d)
) t
I think you should consider changing database structure. You could add table visits and visit_id into your checkins table. Each time you want to register new checkin you check if there is any checkin a day back. If yes then you add a new checkin with visit_id from yesterday's checkin. If not then you add new visit to visits and new checkin with new visit_id.
Then you could get you data in one query with something like that:
SELECT COUNT(id) AS number_of_days, COUNT(DISTINCT visit_id) number_of_visits FROM checkin GROUP BY user, city
It's not very optimal but still better than doing anything with current structure and it will work. Also if results can be separate queries it will work very fast.
But of course drawbacks are you will need to change database structure, do some more scripting and convert current data to new structure (i.e. you will need to add visit_id to current data).