i have to table 2 in same structure .i already get the result by using one table using following query
SELECT *,COUNT(Time),
CASE WHEN COUNT(Time) = 1
THEN CASE WHEN Time > '00:00:00' && Time <= '12:15:00'
THEN Time END ELSE MIN(Time)
END as time_in,
CASE WHEN COUNT(Time) = 1
THEN CASE WHEN Time > '12:15:00' && Time <= '23:59:59'
THEN Time END ELSE NULLIF(MAX(Time), MIN(Time))
END as time_out
FROM attendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%'
GROUP BY EnrolledID,Date
this query will time devide in to the 2 part(time in and time out) .it work fine.now want to get the data from anther table also.it also have same structure attendancedata table.
attendancedata table structure
EnrolledID Date Time
23 2019-09-09 16:19:00
53 2019-08-27 08:19:00
tempattendancedata table structure
EnrolledID Date Time
23 2019-09-09 16:19:00
23 2019-09-20 08:19:00
i want get the result consider above table record and then split time in to the two part .how can i do this task? actual requirement is tempattendancedata table data also need considering for the time split
you include the results of your temp table using UNION ALL then, put it in a subquery for your select case statement
SELECT *,COUNT(Time)
,CASE WHEN COUNT(Time) = 1 THEN
CASE WHEN Time > '00:00:00' && Time <= '12:15:00'
THEN Time END ELSE MIN(Time)
END as time_in,
CASE WHEN COUNT(Time) = 1 THEN
CASE WHEN Time > '12:15:00' && Time <= '23:59:59'
THEN Time END ELSE NULLIF(MAX(Time), MIN(Time))
END as time_out
FROM
(SELECT *
FROM attendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%'
UNION ALL
SELECT *
FROM tempattendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%') t1
GROUP BY t1.EnrolledID, t1.Date
Related
I'm trying to find to which shift belongs a datetime field.
Shifts are defines as time, and I have a startingHour and endingHour.
The query
SELECT * FROM shifts WHERE TIME('2009-11-20 06:35:00') BETWEEN '06:00:00' and '19:00:00'
works perfect, but when the shift is set to start 19:00:00 to 06:00:00 and the time is 23:35:00 it doesn't return anything
WHERE TIME('2009-11-20 23:35:00') BETWEEN '19:00:00' and '06:00:00'
that line isn't returning anything though I do have records on the table
Thanks
That's the shifts table.
if I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 18:23:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get the proper result, but when I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get no result.
These are two cases: start time < end time and start time > end time. You need something like this:
where (start_time < end_time and $t >= start_time and $t < end_time)
or (start_time >= end_time and ($t < start_time or $t >= end_time))
Since '19:00:00' is greater than '06:00:00' then:
BETWEEN '19:00:00' and '06:00:00'
returns 0 (false) and you get no rows.
One way to get the results that you want is to use CASE like this:
.................
AND 1 = CASE
WHEN a.horaInicio <= a.horaFin THEN TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin
ELSE (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND '23:59:59')
OR (TIME('2019-11-22 02:45:00') BETWEEN '00:00:00' AND a.horaFin)
END
I have this table :
id device createdAt type
1 700 2018-09-06 10:00:00 atos
2 700 2018-09-06 09:30:00 farkos
The idea is to verify if in last x hours I have in this table only data type = atos.
For that case I want to get a false as result. If this table will have only type = atos in last h hours the result expected should be true.
I tried like this and after that check with php but not very good idea (I want to do that in sql only) whitout additional treatment :
SELECT * FROM table t
WHERE t.device = 700
AND t.createdAt >= '2018-09-05 11:00:00'
This query will do what you want. It looks at all the entries in the time specified and if any of them is not atos, will return 0 (false). Otherwise it will return 1 (true).
SELECT MIN(CASE WHEN t.type != 'atos' THEN 0 ELSE 1 END)
FROM table1 t
WHERE t.device = 700 AND t.createdAt >= '2018-09-05 11:00:00'
If you want to check for the last h hours, change the WHERE clause to
WHERE t.device = 700 AND t.createdAt >= NOW() - INTERVAL h HOUR
Use case when:
SELECT deviceid,case when min(case when type='atos' then 1 else 0 end)=0 then false when min(case when type='atos' then 1 else 0 end)=1 then true end as val FROM table
t
WHERE t.device = 700
AND t.createdAt >= '2018-09-05 11:00:00'
group by deviceid
I'm using mysql. I have a table time_record. There are four columns namely id, time_in, time_out and students_id. I only want to query the record of the latest time_in of the student but if the latest record of the student is in time_out, it will not query. How do I query the student's latest time_in record if given that the student still does not times out?
Thanks
Here is my sample code (though it returns both records from timein and timeout)
select concat (
st.student_fname,
' ',
st.student_lname
) as 'Name',
t.students_id,
t.time_in,
t.time_out,
case
when t.time_in > t.time_out
then t.time_in
else t.time_out
end as MostRecentDate
from classes c
join student_classes s on c.id = s.classes_id
join timerecords t on t.students_id = s.students_id
join students st on s.students_id = st.student_id
where c.employees_id = 'sessionvalue2'
and
where date (t.time_in) between date (now()) and date (now())
From what I understand, you want to query all results in which time_in is the latest entry and exclude results where time_out is the latest entry.
Try this:
SELECT DISTINCT(tr.id), tr.time_in
FROM time_record tr
WHERE tr.time_in > tr.time_out
ORDER BY tr.time_in DESC
I think your time_out columns may have nulls due to which the comparison results in false in that case and it return time_out.
Also,
date (t.time_in) between date (now()) and date (now())
doesn't make any sense. If you want to check the time_in for today's date, do:
date(t.time_in) = curdate();
Use curdate() instead of date(now()).
or Sargable so that index can be used if any:
date >= curdate()
and date < date_add(curdate(), interval 1 day)
Try flipping the condition like this:
select concat (
st.student_fname,
' ',
st.student_lname
) as 'Name',
t.students_id,
t.time_in,
t.time_out,
case
when t.time_in < t.time_out
then t.time_out
else t.time_in
end as MostRecentDate
from classes c
join student_classes s on c.id = s.classes_id
join timerecords t on t.students_id = s.students_id
join students st on s.students_id = st.student_id
where c.employees_id = 'sessionvalue2'
and date >= curdate()
and date < date_add(curdate(), interval 1 day)
I have check room is exists between two time for particular date.
I have try following two query its run some time rights but, when i select 10:00 AM to 12:00 PM at time wrong results means not return any records.
QUERY-1 :
SELECT 1 FROM `timetable_details` WHERE (
((`td_from` <= '10:00:00') AND (`td_to` > '10:00:00'))
OR
((`td_from` < '12:20:00') AND (`td_to` >= '12:20:00'))
) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))
QUERY-2 :
SELECT 1 FROM `timetable_details` WHERE (
(`td_from` > '07:00:00') AND (`td_to` < '08:00:00')
) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))
I have get td_id = 4 number row but is not returns.
You can use between with OR condition for both columns as below :
SELECT 1 FROM `timetable_details` WHERE (((((`td_from` BETWEEN '10:00:00' AND '12:30:00') OR (`td_to` BETWEEN '10:00:00' AND '12:30:00')) AND ((`td_room`='1') AND (`td_date`='2016-01-25') AND (`td_status` IS NULL))) AND (`td_from` <> '12:30:00')) AND (`td_to` <> '10:00:00'))
I have a MySQL table where employee login and logout timings are recorded. Here in the in-out column 1-represents login and 0-represents logout.
[id] [User_id] [Date_time] [in_out]
1 1 2011-01-20 18:01:03 1
2 1 2011-01-20 19:30:43 0
3 1 2011-01-20 20:46:23 1
4 1 2011-01-21 00:42:45 0
Is it possible to retrieve total hours worked in a day (between 2 days) by a user using single query?
The same Question it's a copy of Get total hours worked in a day mysql and solution:
SELECT `User_id`, time(sum(`Date_time`*(1-2*`in_out`)))
FROM `whatever_table` GROUP BY `User_id`;
But the solution needs to be different when the employee start working in a day and go out on the next day.
You can achieve this using given stored procedure. Consider your table name EventLog.
DELIMITER $$
CREATE PROCEDURE `GET_TOTAL_LOGIN_TIME`(
IN startDate DATETIME,
IN endDate DATETIME,
IN userId INT(11)
)
BEGIN
select
(sum(
case when(e2.Date_time <= startDate) then 0 else
case when(e1.Date_time >= endDate) then 0 else
case when(e1.Date_time >= startDate && e2.Date_time <= endDate) then
TIME_TO_SEC(TIMEDIFF(e2.Date_time, e1.Date_time))/60 else
case when(e1.Date_time <= startDate && e2.Date_time <= endDate) then
TIME_TO_SEC(TIMEDIFF(e2.Date_time, startDate))/60 else
case when(e1.Date_time >= startDate && e2.Date_time >= endDate) then
TIME_TO_SEC(TIMEDIFF(endDate,e1.Date_time))/60
end end end end end
)) as loginTimeInMin
from
((EventLog e1
left join EventLog e2 ON (((e1.User_id = e2.User_id) and (e2.in_out = 0) and (e1.Date_time < e2.Date_time))))
left join EventLog e3 ON (((e1.User_id = e3.User_id) and (e1.Date_time < e3.Date_time) and (e3.Date_time < e2.Date_time))))
where
((e1.in_out = 1) and isnull(e3.Date_time)) and e2.Date_time is not null
AND e1.User_id = userId
AND userRole.userRoleId = roleId
AND userRole.userLoginId = userId
group by e1.User_id;
END;
You can get the number of seconds worked like this:
SELECT `User_id`, sum(unix_timestamp(`Date_time`)*(1-2*`in_out`))
FROM `whatever_table`
GROUP BY `User_id`;
Then you can convert the seconds to whatever you want.