Calculate Total Working Minutes in MySqlDatabase Design: What is the best structure for storing site offices...

Why don't hotels offer ≥ 1 kitchen that must be booked?

if else in jq is not giving expected output

Centering, linebreaks and raggedright in underbrace text in mathmode

How to not forget my phone in the bathroom?

Limit involving inverse functions

How bad is a Computer Science course that doesn't teach Design Patterns?

Why does Python copy numpy arrays where the length of the dimensions are the same?

Badly designed reimbursement form. What does that say about the company?

Run a command that requires sudo after a time has passed

How to write a character over another character

Can a rabbi conduct a marriage if the bride is already pregnant from the groom?

Would life expectancy increase if we replaced healthy organs with artificial ones?

pointer value change C++

The totem pole can be grouped into

A dragon's soul trapped in a ring of mind shielding wants a new body; what magic could enable her to do so?

Why don't the fuse connections in programmable ROM cause the whole matrix to be shorted together?

Identical projects by students at two different colleges: still plagiarism?

Father gets chickenpox, but doesn't infect his two children. How is this possible?

How to place a kinked arrow above a matrix?

How can I learn to care less because it makes me sick?

Sing Baby Shark

Sci fi book, man buys a beat up spaceship and intervenes in a civil war on a planet and eventually becomes a space cop

Headless horseman claims new head

Mathematica seems confused about Kilograms vs KilogramsForce



Calculate Total Working Minutes in MySql


Database Design: What is the best structure for storing site offices and locations?How to calculate total ON hours and Total OFF hours in day of a motor using PHP and MySQLHow to relate 2 user profile tables to a single login table?building structure for employee office relationTotal hours worked per employee per dayTotal hours worked per employee per day including overnight sessionsMySQL - Finding Gaps of Date Ranges - Overlapping DatesHoursworked/day computation via Bio LogsGenerate Slot Summaries(chunks of continuous slots) from the Slot tableHow can I get the last second of the last month as a default parameter value?













0















I have a design a table for attendance and here are the genral fileds




  1. AttendanceMachineLoginId

  2. EmpId

  3. AttendanceDateTime


Whenever user will come to office, he has to make his attendance.



The first attendance will consider login and the second will consider the logout. Each time a record will be added with the time. A user can make multiple entries (login logout in a single day. Like this



enter image description here



EmpId 81 has login and logout two times in same day.



Now, My aim is to generate per day employee report that how many minutes he has given to the company. I just came to know that TIMESTAMPDIFF() can provide the minutes but i am unable to understand that how can I apply it to my table. Additionally, I want to ask that, do the table Fields are right for the desired report or I need to change it?



One Another Strategy: I was also thinking that I should add minutes column in the table and whenever user logout I should calculate the minutes and add that minutes with logout entry.



Sample Data:



INSERT INTO `attendancemachinelogin` (`AttendanceMachineLoginId`, `EmpId`, `TimeTrackId`, `AttendanceDateTime`, `RecordAddDateTime`) VALUES
(0, 81, 315079, '2018-8-15 14:8:46', '2018-08-15 14:09:25'),
(0, 81, 315079, '2018-8-15 14:20:38', '2018-08-15 14:21:17'),
(0, 81, 315079, '2018-8-15 14:21:9', '2018-08-15 14:21:47'),
(0, 81, 315079, '2018-8-15 14:28:37', '2018-08-15 14:29:16'),
(0, 81, 315079, '2018-8-15 14:28:58', '2018-08-15 14:29:36'),
(0, 81, 315079, '2018-8-15 14:36:42', '2018-08-15 14:37:21'),
(0, 81, 315079, '2018-8-15 15:36:34', '2018-08-15 15:37:13'),
(0, 81, 315079, '2018-8-15 15:52:39', '2018-08-15 15:53:17'),
(0, 81, 315079, '2018-8-15 16:5:38', '2018-08-15 16:06:17'),
(0, 81, 315079, '2018-8-15 16:6:50', '2018-08-15 16:07:29'),
(0, 81, 315079, '2018-8-15 16:8:49', '2018-08-15 16:09:29'),
(0, 81, 315079, '2018-8-15 16:18:28', '2018-08-15 16:19:08'),
(0, 81, 315079, '2018-8-15 16:20:49', '2018-08-15 16:21:28'),
(0, 81, 315079, '2018-8-15 16:23:18', '2018-08-15 16:23:58'),
(0, 81, 315079, '2018-8-15 16:24:3', '2018-08-15 16:24:42'),
(0, 81, 315079, '2018-8-15 16:24:47', '2018-08-15 16:25:26'),
(0, 81, 315079, '2018-8-15 16:24:58', '2018-08-15 16:25:37'),
(0, 81, 315079, '2018-8-15 16:25:54', '2018-08-15 16:26:33'),
(0, 81, 315079, '2018-8-15 16:56:47', '2018-08-15 16:57:27'),
(0, 101, 417092, '2018-8-15 17:37:53', '2018-08-15 17:38:32'),
(0, 101, 417092, '2018-8-15 18:4:34', '2018-08-15 18:05:14'),
(0, 101, 417092, '2018-8-15 18:7:43', '2018-08-15 18:08:22'),
(0, 81, 315079, '2018-8-15 18:13:15', '2018-08-15 18:13:54'),
(0, 81, 315079, '2018-8-17 10:50:16', '2018-08-17 10:50:54'),
(0, 101, 417092, '2018-8-17 10:51:54', '2018-08-17 10:52:31'),
(0, 4, 413034, '2018-8-17 11:45:16', '2018-08-17 11:45:54'),
(0, 91, 916086, '2018-8-17 11:59:34', '2018-08-17 12:00:12'),
(0, 81, 315079, '2018-8-17 12:0:19', '2018-08-17 12:00:56'),
(0, 81, 315079, '2018-8-17 15:7:41', '2018-08-17 15:08:17'),
(0, 101, 417092, '2018-8-17 15:9:54', '2018-08-17 15:10:32'),
(0, 101, 417092, '2018-8-17 15:10:9', '2018-08-17 15:10:45'),
(0, 101, 417092, '2018-8-17 15:10:23', '2018-08-17 15:10:59'),
(0, 101, 417092, '2018-8-17 15:10:25', '2018-08-17 15:11:02'),
(0, 101, 417092, '2018-8-17 15:11:6', '2018-08-17 15:11:43'),
(0, 101, 417092, '2018-8-17 15:11:15', '2018-08-17 15:11:52'),
(0, 101, 417092, '2018-8-17 15:11:17', '2018-08-17 15:11:54'),
(0, 81, 315079, '2018-8-17 15:11:32', '2018-08-17 15:12:09'),
(0, 81, 315079, '2018-8-17 15:12:32', '2018-08-17 15:13:09'),
(0, 81, 315079, '2018-8-17 15:35:33', '2018-08-17 15:36:10'),
(0, 81, 315079, '2018-8-17 15:41:58', '2018-08-17 15:42:34'),
(0, 81, 315079, '2018-8-17 15:42:17', '2018-08-17 15:42:54'),
(0, 81, 315079, '2018-8-17 16:8:25', '2018-08-17 16:09:01'),
(0, 81, 315079, '2018-8-17 16:8:32', '2018-08-17 16:09:08'),
(0, 101, 417092, '2018-8-17 16:8:53', '2018-08-17 16:09:30'),
(0, 101, 417092, '2018-8-17 16:9:20', '2018-08-17 16:09:57'),
(0, 4, 413034, '2018-8-17 16:10:16', '2018-08-17 16:10:53'),
(0, 36, 413037, '2018-8-17 16:10:46', '2018-08-17 16:11:23'),
(0, 81, 315079, '2018-8-17 16:22:21', '2018-08-17 16:22:58'),
(0, 101, 417092, '2018-8-17 16:22:45', '2018-08-17 16:23:21'),
(0, 4, 413034, '2018-8-17 16:23:12', '2018-08-17 16:23:49'),
(0, 81, 315079, '2018-8-17 16:23:35', '2018-08-17 16:24:12'),
(0, 81, 315079, '2018-8-17 16:44:4', '2018-08-17 16:44:42'),
(0, 101, 417092, '2018-8-17 16:44:22', '2018-08-17 16:44:58'),
(0, 81, 315079, '2018-8-17 17:6:51', '2018-08-17 17:07:28'),
(0, 101, 417092, '2018-8-17 17:7:8', '2018-08-17 17:07:45'),
(0, 4, 413034, '2018-8-17 17:7:52', '2018-08-17 17:08:28'),
(0, 81, 315079, '2018-8-17 17:9:25', '2018-08-17 17:10:02'),
(0, 101, 417092, '2018-8-17 17:9:46', '2018-08-17 17:10:22'),
(0, 4, 413034, '2018-8-17 17:10:6', '2018-08-17 17:10:42'),
(0, 81, 315079, '2018-8-17 17:10:24', '2018-08-17 17:11:01'),
(0, 81, 315079, '2018-8-17 17:10:39', '2018-08-17 17:11:15'),
(0, 101, 417092, '2018-8-17 17:10:47', '2018-08-17 17:11:24'),
(0, 101, 417092, '2018-8-17 17:10:58', '2018-08-17 17:11:35'),
(0, 81, 315079, '2018-8-17 17:11:10', '2018-08-17 17:11:46'),
(0, 101, 417092, '2018-8-17 17:11:31', '2018-08-17 17:12:09'),
(0, 4, 413034, '2018-8-17 17:40:40', '2018-08-17 17:41:18'),
(0, 101, 417092, '2018-8-17 17:41:23', '2018-08-17 17:41:59'),
(0, 36, 413037, '2018-8-17 17:41:37', '2018-08-17 17:42:14'),
(0, 81, 315079, '2018-8-17 17:42:9', '2018-08-17 17:42:45'),
(0, 3, 213020, '2018-8-17 17:47:34', '2018-08-17 17:48:11'),
(0, 81, 315079, '2018-8-17 17:48:16', '2018-08-17 17:48:52'),
(0, 4, 413034, '2018-8-17 17:48:59', '2018-08-17 17:49:36'),
(0, 4, 413034, '2018-8-17 17:49:59', '2018-08-17 17:50:36'),
(0, 36, 413037, '2018-8-17 17:52:36', '2018-08-17 17:53:13'),
(0, 101, 417092, '2018-8-17 17:52:53', '2018-08-17 17:53:29'),
(0, 6, 213016, '2018-8-17 17:53:30', '2018-08-17 17:54:06'),
(0, 81, 315079, '2018-8-17 17:53:44', '2018-08-17 17:54:20'),
(0, 4, 413034, '2018-8-17 17:54:27', '2018-08-17 17:55:03'),
(0, 3, 213020, '2018-8-17 17:54:49', '2018-08-17 17:55:27'),
(0, 4, 413034, '2018-8-17 17:55:23', '2018-08-17 17:56:00'),
(0, 36, 413037, '2018-8-17 17:58:33', '2018-08-17 17:59:10'),
(0, 101, 417092, '2018-8-17 17:58:47', '2018-08-17 17:59:24'),
(0, 102, 517094, '2018-8-17 17:59:4', '2018-08-17 17:59:40'),
(0, 81, 315079, '2018-8-17 17:59:33', '2018-08-17 18:00:09'),
(0, 4, 413034, '2018-8-17 18:0:16', '2018-08-17 18:00:52'),
(0, 3, 213020, '2018-8-17 18:0:40', '2018-08-17 18:01:17'),
(0, 6, 213016, '2018-8-17 18:1:30', '2018-08-17 18:02:06'),
(0, 36, 413037, '2018-8-17 18:26:24', '2018-08-17 18:27:01'),
(0, 101, 417092, '2018-8-17 18:26:38', '2018-08-17 18:27:14'),
(0, 6, 213016, '2018-8-17 18:27:9', '2018-08-17 18:27:45'),
(0, 81, 315079, '2018-8-17 18:27:24', '2018-08-17 18:28:00'),
(0, 102, 517094, '2018-8-17 18:27:38', '2018-08-17 18:28:14'),
(0, 4, 413034, '2018-8-17 18:28:13', '2018-08-17 18:28:49'),
(0, 81, 315079, '2018-8-17 19:36:49', '2018-08-17 19:37:26'),
(0, 101, 417092, '2018-8-17 19:37:17', '2018-08-17 19:37:54'),
(0, 102, 517094, '2018-8-17 19:37:30', '2018-08-17 19:38:07'),
(0, 36, 413037, '2018-8-17 19:38:13', '2018-08-17 19:38:50'),
(0, 4, 413034, '2018-8-17 19:38:54', '2018-08-17 19:39:32'),
(0, 3, 213020, '2018-8-17 19:39:58', '2018-08-17 19:40:35'),
(0, 101, 417092, '2018-8-18 10:21:26', '2018-08-18 10:22:03'),
(0, 81, 315079, '2018-8-18 10:30:23', '2018-08-18 10:31:09'),
(0, 4, 413034, '2018-8-18 10:31:46', '2018-08-18 10:32:27'),
(0, 102, 517094, '2018-8-18 10:32:15', '2018-08-18 10:32:53'),
(0, 6, 213016, '2018-8-18 10:32:44', '2018-08-18 10:33:22'),
(0, 3, 213020, '2018-8-18 10:33:23', '2018-08-18 10:34:03'),
(0, 81, 315079, '2018-8-18 10:42:49', '2018-08-18 10:43:27'),
(0, 101, 417092, '2018-8-18 10:43:25', '2018-08-18 10:44:03'),
(0, 81, 315079, '2018-8-18 10:48:51', '2018-08-18 10:49:30'),
(0, 102, 517094, '2018-8-18 10:49:9', '2018-08-18 10:49:49'),
(0, 81, 315079, '2018-8-18 10:56:46', '2018-08-18 10:57:25'),
(0, 1, 1211003, '2018-8-18 10:57:0', '2018-08-18 10:57:38'),
(0, 4, 413034, '2018-8-18 10:57:51', '2018-08-18 10:58:38'),
(0, 3, 213020, '2018-8-18 10:58:43', '2018-08-18 10:59:26');









share|improve this question
















bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

    – Michael - sqlbot
    Aug 18 '18 at 23:20













  • Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

    – Rick James
    Aug 26 '18 at 22:29
















0















I have a design a table for attendance and here are the genral fileds




  1. AttendanceMachineLoginId

  2. EmpId

  3. AttendanceDateTime


Whenever user will come to office, he has to make his attendance.



The first attendance will consider login and the second will consider the logout. Each time a record will be added with the time. A user can make multiple entries (login logout in a single day. Like this



enter image description here



EmpId 81 has login and logout two times in same day.



Now, My aim is to generate per day employee report that how many minutes he has given to the company. I just came to know that TIMESTAMPDIFF() can provide the minutes but i am unable to understand that how can I apply it to my table. Additionally, I want to ask that, do the table Fields are right for the desired report or I need to change it?



One Another Strategy: I was also thinking that I should add minutes column in the table and whenever user logout I should calculate the minutes and add that minutes with logout entry.



Sample Data:



INSERT INTO `attendancemachinelogin` (`AttendanceMachineLoginId`, `EmpId`, `TimeTrackId`, `AttendanceDateTime`, `RecordAddDateTime`) VALUES
(0, 81, 315079, '2018-8-15 14:8:46', '2018-08-15 14:09:25'),
(0, 81, 315079, '2018-8-15 14:20:38', '2018-08-15 14:21:17'),
(0, 81, 315079, '2018-8-15 14:21:9', '2018-08-15 14:21:47'),
(0, 81, 315079, '2018-8-15 14:28:37', '2018-08-15 14:29:16'),
(0, 81, 315079, '2018-8-15 14:28:58', '2018-08-15 14:29:36'),
(0, 81, 315079, '2018-8-15 14:36:42', '2018-08-15 14:37:21'),
(0, 81, 315079, '2018-8-15 15:36:34', '2018-08-15 15:37:13'),
(0, 81, 315079, '2018-8-15 15:52:39', '2018-08-15 15:53:17'),
(0, 81, 315079, '2018-8-15 16:5:38', '2018-08-15 16:06:17'),
(0, 81, 315079, '2018-8-15 16:6:50', '2018-08-15 16:07:29'),
(0, 81, 315079, '2018-8-15 16:8:49', '2018-08-15 16:09:29'),
(0, 81, 315079, '2018-8-15 16:18:28', '2018-08-15 16:19:08'),
(0, 81, 315079, '2018-8-15 16:20:49', '2018-08-15 16:21:28'),
(0, 81, 315079, '2018-8-15 16:23:18', '2018-08-15 16:23:58'),
(0, 81, 315079, '2018-8-15 16:24:3', '2018-08-15 16:24:42'),
(0, 81, 315079, '2018-8-15 16:24:47', '2018-08-15 16:25:26'),
(0, 81, 315079, '2018-8-15 16:24:58', '2018-08-15 16:25:37'),
(0, 81, 315079, '2018-8-15 16:25:54', '2018-08-15 16:26:33'),
(0, 81, 315079, '2018-8-15 16:56:47', '2018-08-15 16:57:27'),
(0, 101, 417092, '2018-8-15 17:37:53', '2018-08-15 17:38:32'),
(0, 101, 417092, '2018-8-15 18:4:34', '2018-08-15 18:05:14'),
(0, 101, 417092, '2018-8-15 18:7:43', '2018-08-15 18:08:22'),
(0, 81, 315079, '2018-8-15 18:13:15', '2018-08-15 18:13:54'),
(0, 81, 315079, '2018-8-17 10:50:16', '2018-08-17 10:50:54'),
(0, 101, 417092, '2018-8-17 10:51:54', '2018-08-17 10:52:31'),
(0, 4, 413034, '2018-8-17 11:45:16', '2018-08-17 11:45:54'),
(0, 91, 916086, '2018-8-17 11:59:34', '2018-08-17 12:00:12'),
(0, 81, 315079, '2018-8-17 12:0:19', '2018-08-17 12:00:56'),
(0, 81, 315079, '2018-8-17 15:7:41', '2018-08-17 15:08:17'),
(0, 101, 417092, '2018-8-17 15:9:54', '2018-08-17 15:10:32'),
(0, 101, 417092, '2018-8-17 15:10:9', '2018-08-17 15:10:45'),
(0, 101, 417092, '2018-8-17 15:10:23', '2018-08-17 15:10:59'),
(0, 101, 417092, '2018-8-17 15:10:25', '2018-08-17 15:11:02'),
(0, 101, 417092, '2018-8-17 15:11:6', '2018-08-17 15:11:43'),
(0, 101, 417092, '2018-8-17 15:11:15', '2018-08-17 15:11:52'),
(0, 101, 417092, '2018-8-17 15:11:17', '2018-08-17 15:11:54'),
(0, 81, 315079, '2018-8-17 15:11:32', '2018-08-17 15:12:09'),
(0, 81, 315079, '2018-8-17 15:12:32', '2018-08-17 15:13:09'),
(0, 81, 315079, '2018-8-17 15:35:33', '2018-08-17 15:36:10'),
(0, 81, 315079, '2018-8-17 15:41:58', '2018-08-17 15:42:34'),
(0, 81, 315079, '2018-8-17 15:42:17', '2018-08-17 15:42:54'),
(0, 81, 315079, '2018-8-17 16:8:25', '2018-08-17 16:09:01'),
(0, 81, 315079, '2018-8-17 16:8:32', '2018-08-17 16:09:08'),
(0, 101, 417092, '2018-8-17 16:8:53', '2018-08-17 16:09:30'),
(0, 101, 417092, '2018-8-17 16:9:20', '2018-08-17 16:09:57'),
(0, 4, 413034, '2018-8-17 16:10:16', '2018-08-17 16:10:53'),
(0, 36, 413037, '2018-8-17 16:10:46', '2018-08-17 16:11:23'),
(0, 81, 315079, '2018-8-17 16:22:21', '2018-08-17 16:22:58'),
(0, 101, 417092, '2018-8-17 16:22:45', '2018-08-17 16:23:21'),
(0, 4, 413034, '2018-8-17 16:23:12', '2018-08-17 16:23:49'),
(0, 81, 315079, '2018-8-17 16:23:35', '2018-08-17 16:24:12'),
(0, 81, 315079, '2018-8-17 16:44:4', '2018-08-17 16:44:42'),
(0, 101, 417092, '2018-8-17 16:44:22', '2018-08-17 16:44:58'),
(0, 81, 315079, '2018-8-17 17:6:51', '2018-08-17 17:07:28'),
(0, 101, 417092, '2018-8-17 17:7:8', '2018-08-17 17:07:45'),
(0, 4, 413034, '2018-8-17 17:7:52', '2018-08-17 17:08:28'),
(0, 81, 315079, '2018-8-17 17:9:25', '2018-08-17 17:10:02'),
(0, 101, 417092, '2018-8-17 17:9:46', '2018-08-17 17:10:22'),
(0, 4, 413034, '2018-8-17 17:10:6', '2018-08-17 17:10:42'),
(0, 81, 315079, '2018-8-17 17:10:24', '2018-08-17 17:11:01'),
(0, 81, 315079, '2018-8-17 17:10:39', '2018-08-17 17:11:15'),
(0, 101, 417092, '2018-8-17 17:10:47', '2018-08-17 17:11:24'),
(0, 101, 417092, '2018-8-17 17:10:58', '2018-08-17 17:11:35'),
(0, 81, 315079, '2018-8-17 17:11:10', '2018-08-17 17:11:46'),
(0, 101, 417092, '2018-8-17 17:11:31', '2018-08-17 17:12:09'),
(0, 4, 413034, '2018-8-17 17:40:40', '2018-08-17 17:41:18'),
(0, 101, 417092, '2018-8-17 17:41:23', '2018-08-17 17:41:59'),
(0, 36, 413037, '2018-8-17 17:41:37', '2018-08-17 17:42:14'),
(0, 81, 315079, '2018-8-17 17:42:9', '2018-08-17 17:42:45'),
(0, 3, 213020, '2018-8-17 17:47:34', '2018-08-17 17:48:11'),
(0, 81, 315079, '2018-8-17 17:48:16', '2018-08-17 17:48:52'),
(0, 4, 413034, '2018-8-17 17:48:59', '2018-08-17 17:49:36'),
(0, 4, 413034, '2018-8-17 17:49:59', '2018-08-17 17:50:36'),
(0, 36, 413037, '2018-8-17 17:52:36', '2018-08-17 17:53:13'),
(0, 101, 417092, '2018-8-17 17:52:53', '2018-08-17 17:53:29'),
(0, 6, 213016, '2018-8-17 17:53:30', '2018-08-17 17:54:06'),
(0, 81, 315079, '2018-8-17 17:53:44', '2018-08-17 17:54:20'),
(0, 4, 413034, '2018-8-17 17:54:27', '2018-08-17 17:55:03'),
(0, 3, 213020, '2018-8-17 17:54:49', '2018-08-17 17:55:27'),
(0, 4, 413034, '2018-8-17 17:55:23', '2018-08-17 17:56:00'),
(0, 36, 413037, '2018-8-17 17:58:33', '2018-08-17 17:59:10'),
(0, 101, 417092, '2018-8-17 17:58:47', '2018-08-17 17:59:24'),
(0, 102, 517094, '2018-8-17 17:59:4', '2018-08-17 17:59:40'),
(0, 81, 315079, '2018-8-17 17:59:33', '2018-08-17 18:00:09'),
(0, 4, 413034, '2018-8-17 18:0:16', '2018-08-17 18:00:52'),
(0, 3, 213020, '2018-8-17 18:0:40', '2018-08-17 18:01:17'),
(0, 6, 213016, '2018-8-17 18:1:30', '2018-08-17 18:02:06'),
(0, 36, 413037, '2018-8-17 18:26:24', '2018-08-17 18:27:01'),
(0, 101, 417092, '2018-8-17 18:26:38', '2018-08-17 18:27:14'),
(0, 6, 213016, '2018-8-17 18:27:9', '2018-08-17 18:27:45'),
(0, 81, 315079, '2018-8-17 18:27:24', '2018-08-17 18:28:00'),
(0, 102, 517094, '2018-8-17 18:27:38', '2018-08-17 18:28:14'),
(0, 4, 413034, '2018-8-17 18:28:13', '2018-08-17 18:28:49'),
(0, 81, 315079, '2018-8-17 19:36:49', '2018-08-17 19:37:26'),
(0, 101, 417092, '2018-8-17 19:37:17', '2018-08-17 19:37:54'),
(0, 102, 517094, '2018-8-17 19:37:30', '2018-08-17 19:38:07'),
(0, 36, 413037, '2018-8-17 19:38:13', '2018-08-17 19:38:50'),
(0, 4, 413034, '2018-8-17 19:38:54', '2018-08-17 19:39:32'),
(0, 3, 213020, '2018-8-17 19:39:58', '2018-08-17 19:40:35'),
(0, 101, 417092, '2018-8-18 10:21:26', '2018-08-18 10:22:03'),
(0, 81, 315079, '2018-8-18 10:30:23', '2018-08-18 10:31:09'),
(0, 4, 413034, '2018-8-18 10:31:46', '2018-08-18 10:32:27'),
(0, 102, 517094, '2018-8-18 10:32:15', '2018-08-18 10:32:53'),
(0, 6, 213016, '2018-8-18 10:32:44', '2018-08-18 10:33:22'),
(0, 3, 213020, '2018-8-18 10:33:23', '2018-08-18 10:34:03'),
(0, 81, 315079, '2018-8-18 10:42:49', '2018-08-18 10:43:27'),
(0, 101, 417092, '2018-8-18 10:43:25', '2018-08-18 10:44:03'),
(0, 81, 315079, '2018-8-18 10:48:51', '2018-08-18 10:49:30'),
(0, 102, 517094, '2018-8-18 10:49:9', '2018-08-18 10:49:49'),
(0, 81, 315079, '2018-8-18 10:56:46', '2018-08-18 10:57:25'),
(0, 1, 1211003, '2018-8-18 10:57:0', '2018-08-18 10:57:38'),
(0, 4, 413034, '2018-8-18 10:57:51', '2018-08-18 10:58:38'),
(0, 3, 213020, '2018-8-18 10:58:43', '2018-08-18 10:59:26');









share|improve this question
















bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

    – Michael - sqlbot
    Aug 18 '18 at 23:20













  • Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

    – Rick James
    Aug 26 '18 at 22:29














0












0








0








I have a design a table for attendance and here are the genral fileds




  1. AttendanceMachineLoginId

  2. EmpId

  3. AttendanceDateTime


Whenever user will come to office, he has to make his attendance.



The first attendance will consider login and the second will consider the logout. Each time a record will be added with the time. A user can make multiple entries (login logout in a single day. Like this



enter image description here



EmpId 81 has login and logout two times in same day.



Now, My aim is to generate per day employee report that how many minutes he has given to the company. I just came to know that TIMESTAMPDIFF() can provide the minutes but i am unable to understand that how can I apply it to my table. Additionally, I want to ask that, do the table Fields are right for the desired report or I need to change it?



One Another Strategy: I was also thinking that I should add minutes column in the table and whenever user logout I should calculate the minutes and add that minutes with logout entry.



Sample Data:



INSERT INTO `attendancemachinelogin` (`AttendanceMachineLoginId`, `EmpId`, `TimeTrackId`, `AttendanceDateTime`, `RecordAddDateTime`) VALUES
(0, 81, 315079, '2018-8-15 14:8:46', '2018-08-15 14:09:25'),
(0, 81, 315079, '2018-8-15 14:20:38', '2018-08-15 14:21:17'),
(0, 81, 315079, '2018-8-15 14:21:9', '2018-08-15 14:21:47'),
(0, 81, 315079, '2018-8-15 14:28:37', '2018-08-15 14:29:16'),
(0, 81, 315079, '2018-8-15 14:28:58', '2018-08-15 14:29:36'),
(0, 81, 315079, '2018-8-15 14:36:42', '2018-08-15 14:37:21'),
(0, 81, 315079, '2018-8-15 15:36:34', '2018-08-15 15:37:13'),
(0, 81, 315079, '2018-8-15 15:52:39', '2018-08-15 15:53:17'),
(0, 81, 315079, '2018-8-15 16:5:38', '2018-08-15 16:06:17'),
(0, 81, 315079, '2018-8-15 16:6:50', '2018-08-15 16:07:29'),
(0, 81, 315079, '2018-8-15 16:8:49', '2018-08-15 16:09:29'),
(0, 81, 315079, '2018-8-15 16:18:28', '2018-08-15 16:19:08'),
(0, 81, 315079, '2018-8-15 16:20:49', '2018-08-15 16:21:28'),
(0, 81, 315079, '2018-8-15 16:23:18', '2018-08-15 16:23:58'),
(0, 81, 315079, '2018-8-15 16:24:3', '2018-08-15 16:24:42'),
(0, 81, 315079, '2018-8-15 16:24:47', '2018-08-15 16:25:26'),
(0, 81, 315079, '2018-8-15 16:24:58', '2018-08-15 16:25:37'),
(0, 81, 315079, '2018-8-15 16:25:54', '2018-08-15 16:26:33'),
(0, 81, 315079, '2018-8-15 16:56:47', '2018-08-15 16:57:27'),
(0, 101, 417092, '2018-8-15 17:37:53', '2018-08-15 17:38:32'),
(0, 101, 417092, '2018-8-15 18:4:34', '2018-08-15 18:05:14'),
(0, 101, 417092, '2018-8-15 18:7:43', '2018-08-15 18:08:22'),
(0, 81, 315079, '2018-8-15 18:13:15', '2018-08-15 18:13:54'),
(0, 81, 315079, '2018-8-17 10:50:16', '2018-08-17 10:50:54'),
(0, 101, 417092, '2018-8-17 10:51:54', '2018-08-17 10:52:31'),
(0, 4, 413034, '2018-8-17 11:45:16', '2018-08-17 11:45:54'),
(0, 91, 916086, '2018-8-17 11:59:34', '2018-08-17 12:00:12'),
(0, 81, 315079, '2018-8-17 12:0:19', '2018-08-17 12:00:56'),
(0, 81, 315079, '2018-8-17 15:7:41', '2018-08-17 15:08:17'),
(0, 101, 417092, '2018-8-17 15:9:54', '2018-08-17 15:10:32'),
(0, 101, 417092, '2018-8-17 15:10:9', '2018-08-17 15:10:45'),
(0, 101, 417092, '2018-8-17 15:10:23', '2018-08-17 15:10:59'),
(0, 101, 417092, '2018-8-17 15:10:25', '2018-08-17 15:11:02'),
(0, 101, 417092, '2018-8-17 15:11:6', '2018-08-17 15:11:43'),
(0, 101, 417092, '2018-8-17 15:11:15', '2018-08-17 15:11:52'),
(0, 101, 417092, '2018-8-17 15:11:17', '2018-08-17 15:11:54'),
(0, 81, 315079, '2018-8-17 15:11:32', '2018-08-17 15:12:09'),
(0, 81, 315079, '2018-8-17 15:12:32', '2018-08-17 15:13:09'),
(0, 81, 315079, '2018-8-17 15:35:33', '2018-08-17 15:36:10'),
(0, 81, 315079, '2018-8-17 15:41:58', '2018-08-17 15:42:34'),
(0, 81, 315079, '2018-8-17 15:42:17', '2018-08-17 15:42:54'),
(0, 81, 315079, '2018-8-17 16:8:25', '2018-08-17 16:09:01'),
(0, 81, 315079, '2018-8-17 16:8:32', '2018-08-17 16:09:08'),
(0, 101, 417092, '2018-8-17 16:8:53', '2018-08-17 16:09:30'),
(0, 101, 417092, '2018-8-17 16:9:20', '2018-08-17 16:09:57'),
(0, 4, 413034, '2018-8-17 16:10:16', '2018-08-17 16:10:53'),
(0, 36, 413037, '2018-8-17 16:10:46', '2018-08-17 16:11:23'),
(0, 81, 315079, '2018-8-17 16:22:21', '2018-08-17 16:22:58'),
(0, 101, 417092, '2018-8-17 16:22:45', '2018-08-17 16:23:21'),
(0, 4, 413034, '2018-8-17 16:23:12', '2018-08-17 16:23:49'),
(0, 81, 315079, '2018-8-17 16:23:35', '2018-08-17 16:24:12'),
(0, 81, 315079, '2018-8-17 16:44:4', '2018-08-17 16:44:42'),
(0, 101, 417092, '2018-8-17 16:44:22', '2018-08-17 16:44:58'),
(0, 81, 315079, '2018-8-17 17:6:51', '2018-08-17 17:07:28'),
(0, 101, 417092, '2018-8-17 17:7:8', '2018-08-17 17:07:45'),
(0, 4, 413034, '2018-8-17 17:7:52', '2018-08-17 17:08:28'),
(0, 81, 315079, '2018-8-17 17:9:25', '2018-08-17 17:10:02'),
(0, 101, 417092, '2018-8-17 17:9:46', '2018-08-17 17:10:22'),
(0, 4, 413034, '2018-8-17 17:10:6', '2018-08-17 17:10:42'),
(0, 81, 315079, '2018-8-17 17:10:24', '2018-08-17 17:11:01'),
(0, 81, 315079, '2018-8-17 17:10:39', '2018-08-17 17:11:15'),
(0, 101, 417092, '2018-8-17 17:10:47', '2018-08-17 17:11:24'),
(0, 101, 417092, '2018-8-17 17:10:58', '2018-08-17 17:11:35'),
(0, 81, 315079, '2018-8-17 17:11:10', '2018-08-17 17:11:46'),
(0, 101, 417092, '2018-8-17 17:11:31', '2018-08-17 17:12:09'),
(0, 4, 413034, '2018-8-17 17:40:40', '2018-08-17 17:41:18'),
(0, 101, 417092, '2018-8-17 17:41:23', '2018-08-17 17:41:59'),
(0, 36, 413037, '2018-8-17 17:41:37', '2018-08-17 17:42:14'),
(0, 81, 315079, '2018-8-17 17:42:9', '2018-08-17 17:42:45'),
(0, 3, 213020, '2018-8-17 17:47:34', '2018-08-17 17:48:11'),
(0, 81, 315079, '2018-8-17 17:48:16', '2018-08-17 17:48:52'),
(0, 4, 413034, '2018-8-17 17:48:59', '2018-08-17 17:49:36'),
(0, 4, 413034, '2018-8-17 17:49:59', '2018-08-17 17:50:36'),
(0, 36, 413037, '2018-8-17 17:52:36', '2018-08-17 17:53:13'),
(0, 101, 417092, '2018-8-17 17:52:53', '2018-08-17 17:53:29'),
(0, 6, 213016, '2018-8-17 17:53:30', '2018-08-17 17:54:06'),
(0, 81, 315079, '2018-8-17 17:53:44', '2018-08-17 17:54:20'),
(0, 4, 413034, '2018-8-17 17:54:27', '2018-08-17 17:55:03'),
(0, 3, 213020, '2018-8-17 17:54:49', '2018-08-17 17:55:27'),
(0, 4, 413034, '2018-8-17 17:55:23', '2018-08-17 17:56:00'),
(0, 36, 413037, '2018-8-17 17:58:33', '2018-08-17 17:59:10'),
(0, 101, 417092, '2018-8-17 17:58:47', '2018-08-17 17:59:24'),
(0, 102, 517094, '2018-8-17 17:59:4', '2018-08-17 17:59:40'),
(0, 81, 315079, '2018-8-17 17:59:33', '2018-08-17 18:00:09'),
(0, 4, 413034, '2018-8-17 18:0:16', '2018-08-17 18:00:52'),
(0, 3, 213020, '2018-8-17 18:0:40', '2018-08-17 18:01:17'),
(0, 6, 213016, '2018-8-17 18:1:30', '2018-08-17 18:02:06'),
(0, 36, 413037, '2018-8-17 18:26:24', '2018-08-17 18:27:01'),
(0, 101, 417092, '2018-8-17 18:26:38', '2018-08-17 18:27:14'),
(0, 6, 213016, '2018-8-17 18:27:9', '2018-08-17 18:27:45'),
(0, 81, 315079, '2018-8-17 18:27:24', '2018-08-17 18:28:00'),
(0, 102, 517094, '2018-8-17 18:27:38', '2018-08-17 18:28:14'),
(0, 4, 413034, '2018-8-17 18:28:13', '2018-08-17 18:28:49'),
(0, 81, 315079, '2018-8-17 19:36:49', '2018-08-17 19:37:26'),
(0, 101, 417092, '2018-8-17 19:37:17', '2018-08-17 19:37:54'),
(0, 102, 517094, '2018-8-17 19:37:30', '2018-08-17 19:38:07'),
(0, 36, 413037, '2018-8-17 19:38:13', '2018-08-17 19:38:50'),
(0, 4, 413034, '2018-8-17 19:38:54', '2018-08-17 19:39:32'),
(0, 3, 213020, '2018-8-17 19:39:58', '2018-08-17 19:40:35'),
(0, 101, 417092, '2018-8-18 10:21:26', '2018-08-18 10:22:03'),
(0, 81, 315079, '2018-8-18 10:30:23', '2018-08-18 10:31:09'),
(0, 4, 413034, '2018-8-18 10:31:46', '2018-08-18 10:32:27'),
(0, 102, 517094, '2018-8-18 10:32:15', '2018-08-18 10:32:53'),
(0, 6, 213016, '2018-8-18 10:32:44', '2018-08-18 10:33:22'),
(0, 3, 213020, '2018-8-18 10:33:23', '2018-08-18 10:34:03'),
(0, 81, 315079, '2018-8-18 10:42:49', '2018-08-18 10:43:27'),
(0, 101, 417092, '2018-8-18 10:43:25', '2018-08-18 10:44:03'),
(0, 81, 315079, '2018-8-18 10:48:51', '2018-08-18 10:49:30'),
(0, 102, 517094, '2018-8-18 10:49:9', '2018-08-18 10:49:49'),
(0, 81, 315079, '2018-8-18 10:56:46', '2018-08-18 10:57:25'),
(0, 1, 1211003, '2018-8-18 10:57:0', '2018-08-18 10:57:38'),
(0, 4, 413034, '2018-8-18 10:57:51', '2018-08-18 10:58:38'),
(0, 3, 213020, '2018-8-18 10:58:43', '2018-08-18 10:59:26');









share|improve this question
















I have a design a table for attendance and here are the genral fileds




  1. AttendanceMachineLoginId

  2. EmpId

  3. AttendanceDateTime


Whenever user will come to office, he has to make his attendance.



The first attendance will consider login and the second will consider the logout. Each time a record will be added with the time. A user can make multiple entries (login logout in a single day. Like this



enter image description here



EmpId 81 has login and logout two times in same day.



Now, My aim is to generate per day employee report that how many minutes he has given to the company. I just came to know that TIMESTAMPDIFF() can provide the minutes but i am unable to understand that how can I apply it to my table. Additionally, I want to ask that, do the table Fields are right for the desired report or I need to change it?



One Another Strategy: I was also thinking that I should add minutes column in the table and whenever user logout I should calculate the minutes and add that minutes with logout entry.



Sample Data:



INSERT INTO `attendancemachinelogin` (`AttendanceMachineLoginId`, `EmpId`, `TimeTrackId`, `AttendanceDateTime`, `RecordAddDateTime`) VALUES
(0, 81, 315079, '2018-8-15 14:8:46', '2018-08-15 14:09:25'),
(0, 81, 315079, '2018-8-15 14:20:38', '2018-08-15 14:21:17'),
(0, 81, 315079, '2018-8-15 14:21:9', '2018-08-15 14:21:47'),
(0, 81, 315079, '2018-8-15 14:28:37', '2018-08-15 14:29:16'),
(0, 81, 315079, '2018-8-15 14:28:58', '2018-08-15 14:29:36'),
(0, 81, 315079, '2018-8-15 14:36:42', '2018-08-15 14:37:21'),
(0, 81, 315079, '2018-8-15 15:36:34', '2018-08-15 15:37:13'),
(0, 81, 315079, '2018-8-15 15:52:39', '2018-08-15 15:53:17'),
(0, 81, 315079, '2018-8-15 16:5:38', '2018-08-15 16:06:17'),
(0, 81, 315079, '2018-8-15 16:6:50', '2018-08-15 16:07:29'),
(0, 81, 315079, '2018-8-15 16:8:49', '2018-08-15 16:09:29'),
(0, 81, 315079, '2018-8-15 16:18:28', '2018-08-15 16:19:08'),
(0, 81, 315079, '2018-8-15 16:20:49', '2018-08-15 16:21:28'),
(0, 81, 315079, '2018-8-15 16:23:18', '2018-08-15 16:23:58'),
(0, 81, 315079, '2018-8-15 16:24:3', '2018-08-15 16:24:42'),
(0, 81, 315079, '2018-8-15 16:24:47', '2018-08-15 16:25:26'),
(0, 81, 315079, '2018-8-15 16:24:58', '2018-08-15 16:25:37'),
(0, 81, 315079, '2018-8-15 16:25:54', '2018-08-15 16:26:33'),
(0, 81, 315079, '2018-8-15 16:56:47', '2018-08-15 16:57:27'),
(0, 101, 417092, '2018-8-15 17:37:53', '2018-08-15 17:38:32'),
(0, 101, 417092, '2018-8-15 18:4:34', '2018-08-15 18:05:14'),
(0, 101, 417092, '2018-8-15 18:7:43', '2018-08-15 18:08:22'),
(0, 81, 315079, '2018-8-15 18:13:15', '2018-08-15 18:13:54'),
(0, 81, 315079, '2018-8-17 10:50:16', '2018-08-17 10:50:54'),
(0, 101, 417092, '2018-8-17 10:51:54', '2018-08-17 10:52:31'),
(0, 4, 413034, '2018-8-17 11:45:16', '2018-08-17 11:45:54'),
(0, 91, 916086, '2018-8-17 11:59:34', '2018-08-17 12:00:12'),
(0, 81, 315079, '2018-8-17 12:0:19', '2018-08-17 12:00:56'),
(0, 81, 315079, '2018-8-17 15:7:41', '2018-08-17 15:08:17'),
(0, 101, 417092, '2018-8-17 15:9:54', '2018-08-17 15:10:32'),
(0, 101, 417092, '2018-8-17 15:10:9', '2018-08-17 15:10:45'),
(0, 101, 417092, '2018-8-17 15:10:23', '2018-08-17 15:10:59'),
(0, 101, 417092, '2018-8-17 15:10:25', '2018-08-17 15:11:02'),
(0, 101, 417092, '2018-8-17 15:11:6', '2018-08-17 15:11:43'),
(0, 101, 417092, '2018-8-17 15:11:15', '2018-08-17 15:11:52'),
(0, 101, 417092, '2018-8-17 15:11:17', '2018-08-17 15:11:54'),
(0, 81, 315079, '2018-8-17 15:11:32', '2018-08-17 15:12:09'),
(0, 81, 315079, '2018-8-17 15:12:32', '2018-08-17 15:13:09'),
(0, 81, 315079, '2018-8-17 15:35:33', '2018-08-17 15:36:10'),
(0, 81, 315079, '2018-8-17 15:41:58', '2018-08-17 15:42:34'),
(0, 81, 315079, '2018-8-17 15:42:17', '2018-08-17 15:42:54'),
(0, 81, 315079, '2018-8-17 16:8:25', '2018-08-17 16:09:01'),
(0, 81, 315079, '2018-8-17 16:8:32', '2018-08-17 16:09:08'),
(0, 101, 417092, '2018-8-17 16:8:53', '2018-08-17 16:09:30'),
(0, 101, 417092, '2018-8-17 16:9:20', '2018-08-17 16:09:57'),
(0, 4, 413034, '2018-8-17 16:10:16', '2018-08-17 16:10:53'),
(0, 36, 413037, '2018-8-17 16:10:46', '2018-08-17 16:11:23'),
(0, 81, 315079, '2018-8-17 16:22:21', '2018-08-17 16:22:58'),
(0, 101, 417092, '2018-8-17 16:22:45', '2018-08-17 16:23:21'),
(0, 4, 413034, '2018-8-17 16:23:12', '2018-08-17 16:23:49'),
(0, 81, 315079, '2018-8-17 16:23:35', '2018-08-17 16:24:12'),
(0, 81, 315079, '2018-8-17 16:44:4', '2018-08-17 16:44:42'),
(0, 101, 417092, '2018-8-17 16:44:22', '2018-08-17 16:44:58'),
(0, 81, 315079, '2018-8-17 17:6:51', '2018-08-17 17:07:28'),
(0, 101, 417092, '2018-8-17 17:7:8', '2018-08-17 17:07:45'),
(0, 4, 413034, '2018-8-17 17:7:52', '2018-08-17 17:08:28'),
(0, 81, 315079, '2018-8-17 17:9:25', '2018-08-17 17:10:02'),
(0, 101, 417092, '2018-8-17 17:9:46', '2018-08-17 17:10:22'),
(0, 4, 413034, '2018-8-17 17:10:6', '2018-08-17 17:10:42'),
(0, 81, 315079, '2018-8-17 17:10:24', '2018-08-17 17:11:01'),
(0, 81, 315079, '2018-8-17 17:10:39', '2018-08-17 17:11:15'),
(0, 101, 417092, '2018-8-17 17:10:47', '2018-08-17 17:11:24'),
(0, 101, 417092, '2018-8-17 17:10:58', '2018-08-17 17:11:35'),
(0, 81, 315079, '2018-8-17 17:11:10', '2018-08-17 17:11:46'),
(0, 101, 417092, '2018-8-17 17:11:31', '2018-08-17 17:12:09'),
(0, 4, 413034, '2018-8-17 17:40:40', '2018-08-17 17:41:18'),
(0, 101, 417092, '2018-8-17 17:41:23', '2018-08-17 17:41:59'),
(0, 36, 413037, '2018-8-17 17:41:37', '2018-08-17 17:42:14'),
(0, 81, 315079, '2018-8-17 17:42:9', '2018-08-17 17:42:45'),
(0, 3, 213020, '2018-8-17 17:47:34', '2018-08-17 17:48:11'),
(0, 81, 315079, '2018-8-17 17:48:16', '2018-08-17 17:48:52'),
(0, 4, 413034, '2018-8-17 17:48:59', '2018-08-17 17:49:36'),
(0, 4, 413034, '2018-8-17 17:49:59', '2018-08-17 17:50:36'),
(0, 36, 413037, '2018-8-17 17:52:36', '2018-08-17 17:53:13'),
(0, 101, 417092, '2018-8-17 17:52:53', '2018-08-17 17:53:29'),
(0, 6, 213016, '2018-8-17 17:53:30', '2018-08-17 17:54:06'),
(0, 81, 315079, '2018-8-17 17:53:44', '2018-08-17 17:54:20'),
(0, 4, 413034, '2018-8-17 17:54:27', '2018-08-17 17:55:03'),
(0, 3, 213020, '2018-8-17 17:54:49', '2018-08-17 17:55:27'),
(0, 4, 413034, '2018-8-17 17:55:23', '2018-08-17 17:56:00'),
(0, 36, 413037, '2018-8-17 17:58:33', '2018-08-17 17:59:10'),
(0, 101, 417092, '2018-8-17 17:58:47', '2018-08-17 17:59:24'),
(0, 102, 517094, '2018-8-17 17:59:4', '2018-08-17 17:59:40'),
(0, 81, 315079, '2018-8-17 17:59:33', '2018-08-17 18:00:09'),
(0, 4, 413034, '2018-8-17 18:0:16', '2018-08-17 18:00:52'),
(0, 3, 213020, '2018-8-17 18:0:40', '2018-08-17 18:01:17'),
(0, 6, 213016, '2018-8-17 18:1:30', '2018-08-17 18:02:06'),
(0, 36, 413037, '2018-8-17 18:26:24', '2018-08-17 18:27:01'),
(0, 101, 417092, '2018-8-17 18:26:38', '2018-08-17 18:27:14'),
(0, 6, 213016, '2018-8-17 18:27:9', '2018-08-17 18:27:45'),
(0, 81, 315079, '2018-8-17 18:27:24', '2018-08-17 18:28:00'),
(0, 102, 517094, '2018-8-17 18:27:38', '2018-08-17 18:28:14'),
(0, 4, 413034, '2018-8-17 18:28:13', '2018-08-17 18:28:49'),
(0, 81, 315079, '2018-8-17 19:36:49', '2018-08-17 19:37:26'),
(0, 101, 417092, '2018-8-17 19:37:17', '2018-08-17 19:37:54'),
(0, 102, 517094, '2018-8-17 19:37:30', '2018-08-17 19:38:07'),
(0, 36, 413037, '2018-8-17 19:38:13', '2018-08-17 19:38:50'),
(0, 4, 413034, '2018-8-17 19:38:54', '2018-08-17 19:39:32'),
(0, 3, 213020, '2018-8-17 19:39:58', '2018-08-17 19:40:35'),
(0, 101, 417092, '2018-8-18 10:21:26', '2018-08-18 10:22:03'),
(0, 81, 315079, '2018-8-18 10:30:23', '2018-08-18 10:31:09'),
(0, 4, 413034, '2018-8-18 10:31:46', '2018-08-18 10:32:27'),
(0, 102, 517094, '2018-8-18 10:32:15', '2018-08-18 10:32:53'),
(0, 6, 213016, '2018-8-18 10:32:44', '2018-08-18 10:33:22'),
(0, 3, 213020, '2018-8-18 10:33:23', '2018-08-18 10:34:03'),
(0, 81, 315079, '2018-8-18 10:42:49', '2018-08-18 10:43:27'),
(0, 101, 417092, '2018-8-18 10:43:25', '2018-08-18 10:44:03'),
(0, 81, 315079, '2018-8-18 10:48:51', '2018-08-18 10:49:30'),
(0, 102, 517094, '2018-8-18 10:49:9', '2018-08-18 10:49:49'),
(0, 81, 315079, '2018-8-18 10:56:46', '2018-08-18 10:57:25'),
(0, 1, 1211003, '2018-8-18 10:57:0', '2018-08-18 10:57:38'),
(0, 4, 413034, '2018-8-18 10:57:51', '2018-08-18 10:58:38'),
(0, 3, 213020, '2018-8-18 10:58:43', '2018-08-18 10:59:26');






mysql datetime timestamp reporting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 '18 at 4:30







Muhammad Faizan Khan

















asked Aug 18 '18 at 7:25









Muhammad Faizan KhanMuhammad Faizan Khan

1063




1063





bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

    – Michael - sqlbot
    Aug 18 '18 at 23:20













  • Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

    – Rick James
    Aug 26 '18 at 22:29



















  • Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

    – Michael - sqlbot
    Aug 18 '18 at 23:20













  • Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

    – Rick James
    Aug 26 '18 at 22:29

















Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

– Michael - sqlbot
Aug 18 '18 at 23:20







Your structure seems incomplete, since it does not indicate whether an event is a login or logout, and assumptions of clean pairing and ordering, and sessions never spanning midnight... seem problematic. You don't need to store minutes, but updating the login row with a logout time or another strategy that captures a more thorough perspective of what is actually happening might be a distinct improvement. Solving this problem with the current schemata with pure declarative SQL (no procs, functions, temp tables, cursors, etc.) seems like it will not be straightforward.

– Michael - sqlbot
Aug 18 '18 at 23:20















Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

– Rick James
Aug 26 '18 at 22:29





Unrelated, but very important: The pairing of login to logout will fail some day. Someone will fail to logout. Or will login twice. Or the system will drop a record. Or...

– Rick James
Aug 26 '18 at 22:29










1 Answer
1






active

oldest

votes


















0














Well, I've made an assumption that first event each day for each employee is the login.



For mysql 8.x having CTEs the query can be the next:



WITH cteB (EID, mins) AS 
(
WITH cteA (EID, TS, cnt) AS
(
SELECT w.EmpId
, w.AttendanceDateTime
, IF( @prevEID != @prevID := w.EmpID, @cnt := 1, @cnt := @cnt+1 ) AS cnt
FROM table AS w
ORDER BY w.EmpId ASC
, w.AttendanceDateTime ASC
)
SELECT z.EID
, TIMESTAMPDIFF(MINUTE, z.TS, q.TS) AS mins
FROM cteA AS z
JOIN cteA AS q ON q.EID = z.EID
AND z.cnt = q.cnt+1
AND z.cnt % 2 = 1
)
SELECT x.EID AS EmpID
, SUM(x.mins) AS Eminutes
FROM cteB AS x
GROUP BY x.EID
ORDER BY x.EID ASC
;


The same result can be achieved in the earlier versions of mysql with subqueries.






share|improve this answer





















  • 1





    The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

    – Philᵀᴹ
    Aug 18 '18 at 12:46











  • This is valueable but i am looking for solution with multiple entries

    – Muhammad Faizan Khan
    Aug 18 '18 at 13:00











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f215253%2fcalculate-total-working-minutes-in-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Well, I've made an assumption that first event each day for each employee is the login.



For mysql 8.x having CTEs the query can be the next:



WITH cteB (EID, mins) AS 
(
WITH cteA (EID, TS, cnt) AS
(
SELECT w.EmpId
, w.AttendanceDateTime
, IF( @prevEID != @prevID := w.EmpID, @cnt := 1, @cnt := @cnt+1 ) AS cnt
FROM table AS w
ORDER BY w.EmpId ASC
, w.AttendanceDateTime ASC
)
SELECT z.EID
, TIMESTAMPDIFF(MINUTE, z.TS, q.TS) AS mins
FROM cteA AS z
JOIN cteA AS q ON q.EID = z.EID
AND z.cnt = q.cnt+1
AND z.cnt % 2 = 1
)
SELECT x.EID AS EmpID
, SUM(x.mins) AS Eminutes
FROM cteB AS x
GROUP BY x.EID
ORDER BY x.EID ASC
;


The same result can be achieved in the earlier versions of mysql with subqueries.






share|improve this answer





















  • 1





    The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

    – Philᵀᴹ
    Aug 18 '18 at 12:46











  • This is valueable but i am looking for solution with multiple entries

    – Muhammad Faizan Khan
    Aug 18 '18 at 13:00
















0














Well, I've made an assumption that first event each day for each employee is the login.



For mysql 8.x having CTEs the query can be the next:



WITH cteB (EID, mins) AS 
(
WITH cteA (EID, TS, cnt) AS
(
SELECT w.EmpId
, w.AttendanceDateTime
, IF( @prevEID != @prevID := w.EmpID, @cnt := 1, @cnt := @cnt+1 ) AS cnt
FROM table AS w
ORDER BY w.EmpId ASC
, w.AttendanceDateTime ASC
)
SELECT z.EID
, TIMESTAMPDIFF(MINUTE, z.TS, q.TS) AS mins
FROM cteA AS z
JOIN cteA AS q ON q.EID = z.EID
AND z.cnt = q.cnt+1
AND z.cnt % 2 = 1
)
SELECT x.EID AS EmpID
, SUM(x.mins) AS Eminutes
FROM cteB AS x
GROUP BY x.EID
ORDER BY x.EID ASC
;


The same result can be achieved in the earlier versions of mysql with subqueries.






share|improve this answer





















  • 1





    The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

    – Philᵀᴹ
    Aug 18 '18 at 12:46











  • This is valueable but i am looking for solution with multiple entries

    – Muhammad Faizan Khan
    Aug 18 '18 at 13:00














0












0








0







Well, I've made an assumption that first event each day for each employee is the login.



For mysql 8.x having CTEs the query can be the next:



WITH cteB (EID, mins) AS 
(
WITH cteA (EID, TS, cnt) AS
(
SELECT w.EmpId
, w.AttendanceDateTime
, IF( @prevEID != @prevID := w.EmpID, @cnt := 1, @cnt := @cnt+1 ) AS cnt
FROM table AS w
ORDER BY w.EmpId ASC
, w.AttendanceDateTime ASC
)
SELECT z.EID
, TIMESTAMPDIFF(MINUTE, z.TS, q.TS) AS mins
FROM cteA AS z
JOIN cteA AS q ON q.EID = z.EID
AND z.cnt = q.cnt+1
AND z.cnt % 2 = 1
)
SELECT x.EID AS EmpID
, SUM(x.mins) AS Eminutes
FROM cteB AS x
GROUP BY x.EID
ORDER BY x.EID ASC
;


The same result can be achieved in the earlier versions of mysql with subqueries.






share|improve this answer















Well, I've made an assumption that first event each day for each employee is the login.



For mysql 8.x having CTEs the query can be the next:



WITH cteB (EID, mins) AS 
(
WITH cteA (EID, TS, cnt) AS
(
SELECT w.EmpId
, w.AttendanceDateTime
, IF( @prevEID != @prevID := w.EmpID, @cnt := 1, @cnt := @cnt+1 ) AS cnt
FROM table AS w
ORDER BY w.EmpId ASC
, w.AttendanceDateTime ASC
)
SELECT z.EID
, TIMESTAMPDIFF(MINUTE, z.TS, q.TS) AS mins
FROM cteA AS z
JOIN cteA AS q ON q.EID = z.EID
AND z.cnt = q.cnt+1
AND z.cnt % 2 = 1
)
SELECT x.EID AS EmpID
, SUM(x.mins) AS Eminutes
FROM cteB AS x
GROUP BY x.EID
ORDER BY x.EID ASC
;


The same result can be achieved in the earlier versions of mysql with subqueries.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 18 '18 at 14:02

























answered Aug 18 '18 at 9:48









KondybasKondybas

2,633912




2,633912








  • 1





    The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

    – Philᵀᴹ
    Aug 18 '18 at 12:46











  • This is valueable but i am looking for solution with multiple entries

    – Muhammad Faizan Khan
    Aug 18 '18 at 13:00














  • 1





    The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

    – Philᵀᴹ
    Aug 18 '18 at 12:46











  • This is valueable but i am looking for solution with multiple entries

    – Muhammad Faizan Khan
    Aug 18 '18 at 13:00








1




1





The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

– Philᵀᴹ
Aug 18 '18 at 12:46





The OP states "EmpId 81 has login and logout two times in same day", so this does not answer the question

– Philᵀᴹ
Aug 18 '18 at 12:46













This is valueable but i am looking for solution with multiple entries

– Muhammad Faizan Khan
Aug 18 '18 at 13:00





This is valueable but i am looking for solution with multiple entries

– Muhammad Faizan Khan
Aug 18 '18 at 13:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Database Administrators Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f215253%2fcalculate-total-working-minutes-in-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

Always On Availability groups resolving state after failover - Remote harden of transaction...

Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...