I'm working in SQL Server Management Studio so I suppose this is a Microsoft SQL Server T-SQL question.
The real world scenario is this: I have multiple employees working across multiple locations and the "time in" and "time out" records for each. I have already created a unique "Shift ID" for each set of time intervals and joined, based on employee, date, and location the shifts of other employees that match my keystone employee, or the one against which I am comparing everyone else.
Furthermore, I have written a query that pulled each "other employee's" specific overlapping time interval with the keystone. For one shift the timeline looks like this:
Key Emp. | 9AM------------------------6PM
Emp. A | 9AM------------------------6PM
Emp. B | 12PM-------4PM
So the discrete periods where a true "controlled" comparison can be made are among:
- Key, A, and B from 12PM - 4PM
- Key and A from 9AM - 12PM
- Key and A from 4PM - 6PM
The end goal is to pull all the activity (organized as events with datetime stamps in a separate table) for each employee that occurs within those time periods and compare totals for each relevant employee. So there would be a separate "Count(events)" total for each time frame only affected by the employees that share the time interval as described above.
Currently, my data is organized like this:
the "In" and "Out" columns for key and other employees are stored as TIMESTAMPs; the "1/1,6PM" is just my crappy way of saving space in my example. Please see my consumable data at the end of this post. SSMS doesn't seem to care that I have more than TIMESTAMP column and treats them all like DATETIME:
Key_ShiftID| Key In | Key Out | Oth_Emp_ShiftID | Oth_Emp_In | Oth_Emp_Out
K 1/1,9AM 1/1,6PM A 1/1,9AM 1/1,6PM
K 1/1,9AM 1/1,6PM B 1/1,12PM 1/1,4PM
Where the Shift IDs (Key_ShiftID and Oth_Emp_ShiftID) are unique strings and the time intervals are defined by two columns a piece (Key_In & Key_Out + Oth_Emp_In & Oth_Emp_Out) are stored as datetime/timestamps. I'm looking for discrete periods where I can compare the activity of the employees, which is in a separate table with each event having a unique datetime as was mentioned earlier. Thus, I think the ending data would look something like this:
Key_ShiftID| Key_In | Key_Out | Oth_Emp_ShiftID | Oth_Emp_In | Oth_Emp_Out
K 1/1,9AM 1/1,6PM A 1/1,12PM 1/1,4PM
K 1/1,9AM 1/1,6PM B 1/1,12PM 1/1,4PM
K 1/1,9AM 1/1,6PM A 1/1,9AM 1/1,12PM
K 1/1,9AM 1/1,6PM A 1/1,4PM 1/1,6PM
So I would be able to join the table above to my activity table by ShiftID and bring in the Count(events) per relevant employee
where event_datetime >= Oth_Emp_In and event_datetime <= Oth_Emp_Out
Additionally, as I noted before, I already wrote a query to cut down the non-key employees' shifts to reflect only the time intervals where they overlap with the key employee, so the Other_Emp_In will always be greater than or equal to the Key In time and Other_Emp_Out will always be less than or equal to the Key Out time.
Thanks in advance. I've been researching and struggling with this for around 2 days.
Here's sample data of one key shift (not the exact example above):
Also, SQL Server doesn't seem to care that I have more than TIMESTAMP column and treats them all like DATETIME.
CREATE TABLE "sample_data"
(
"Employee" INT,
"Key_ShiftID" TEXT,
"Key_In" TIMESTAMP,
"Key_Out" TIMESTAMP,
"Other_Emp_ShiftID" TEXT,
"Other_Emp_In" TIMESTAMP,
"Other_Emp_Out" TIMESTAMP,
"overlap_min" TIMESTAMP,
"overlap_max" TIMESTAMP
);
INSERT INTO "sample_data"
VALUES (900, '545BD826-0C9A-408B-BE9F-4C3D7D307948', '2016-09-27 14:15:00', '2016-09-27 21:45:00', '035FA1C1-B469-44EB-B5B4-5B6948574464', '2016-09-27 08:45:00', '2016-09-27 16:15:00', '2016-09-27 14:15:00', '2016-09-27 16:15:00'),
(78, '545BD826-0C9A-408B-BE9F-4C3D7D307948', '2016-09-27 14:15:00', '2016-09-27 21:45:00', '74035838-FD07-4F8D-8AC4-F6407AC786D9', '2016-09-27 18:00:00', '2016-09-27 21:15:00', '2016-09-27 18:00:00', '2016-09-27 21:15:00'),
(900, '545BD826-0C9A-408B-BE9F-4C3D7D307948', '2016-09-27 14:15:00', '2016-09-27 21:45:00', 'D7E9ADCD-8631-476D-B69F-00626F0E4B06', '2016-09-27 16:45:00', '2016-09-27 21:45:00', '2016-09-27 16:45:00', '2016-09-27 21:45:00');
Welcome to StackOverflow. In the future, try to include some easily consumable sample data like what I am including in my solution below.
This is a fun little problem. For this kind of thing I leverage my patExtract8K function which leverages ngrams8K. Here's an example of how to use PatExtract; here I'm extracting money from a string:
Results:
Now to tackle your problem:
Results:
Note that I have no idea where the "1/1" is coming from so I just hard-coded that in.
Here's my underlying functions. All are very helpful for solving a wide array of SQL issues efficiently and with little code.