I have three tables that share one field with another table, so:
Employee_Data: EMP_ID, PHONE_EXTENSION
Outage_Data: DATE, EMP_ID
Phone_Data: DATE, PHONE_EXTENSION
I need to combine data where the agent data is matched with data in the other two tables. Basically, I need to combine the data from Outage_data
and Phone_data
(because it is all grouped by date) with Employee_Data
as the "bridge".
I already have my SELECT and everything laid out like it needs to be; I just need help with the join.
EDIT By Request, here is the full query. I tried not to do this for simplicity's sake:
With Epi AS
(SELECT EMP_ID, PHONE_EXTENSION, Name, Manager, EWFMDeptname, Location From Employee_Data)
,
Shrink AS
(SELECT DATE, EMP_ID, START_MOMENT, STOP_MOMENT, SEG_CODE FROM Outage_Data)
,
OutIn AS
(SELECT date, logid, login, logout From PHONE_DATA)
SELECT
ISNULL(Shrink.DATE,OutIn.DATE) AS RowDate,
ISNULL(Epi.EMP_ID,RTRIM(LTRIM(Shrink.EMP_ID))) AS Badge,
ISNULL(Epi.PHONE_EXTENSION, OutIn.logid) AS [ACD/Extension],
Epi.Name,
Epi.Manager,
Epi.EWFMDeptName,
Epi.Location,
ISNULL(Shrink.START_MOMENT, (SELECT Shrink.START_MOMENT FROM Shrink WHERE SHRINK.SEG_CODE = 'SHIFT' AND Shrink.EMP_ID = EPI.EMP_ID AND SHRINK.DATE = OutIn.DATE)) AS ShiftStart,
ISNULL(Shrink.STOP_MOMENT, (SELECT Shrink.STOP_MOMENT FROM Shrink WHERE SHRINK.SEG_CODE = 'SHIFT' AND Shrink.EMP_ID = EPI.EMP_ID AND SHRINK.DATE = OutIn.row_date)) AS ShiftStop,
OutIn.login AS Login,
OutIn.logout AS Logout,
Shrink.SEG_CODE AS OUTSEG,
IIF(Shrink.SEG_CODE = 'PABS' OR Shrink.SEG_CODE = 'UABS' OR Shrink.SEG_CODE = 'PPBA' OR Shrink.SEG_CODE = 'UPBA' OR Shrink.SEG_CODE = 'VACA' OR Shrink.SEG_CODE = 'JURY' OR Shrink.SEG_CODE = 'GOVT' OR Shrink.SEG_CODE = 'LOA' OR Shrink.SEG_CODE = 'BRVMNT' OR Shrink.SEG_CODE = 'FH', Shrink.START_MOMENT, NUll) AS OutStart,
IIF(Shrink.SEG_CODE = 'PABS' OR Shrink.SEG_CODE = 'UABS' OR Shrink.SEG_CODE = 'PPBA' OR Shrink.SEG_CODE = 'UPBA' OR Shrink.SEG_CODE = 'VACA' OR Shrink.SEG_CODE = 'JURY' OR Shrink.SEG_CODE = 'GOVT' OR Shrink.SEG_CODE = 'LOA' OR Shrink.SEG_CODE = 'BRVMNT' OR Shrink.SEG_CODE = 'FH', Shrink.STOP_MOMENT, NUll) AS OutStop,
IIF(DateADD(minute, 5, Shrink.START_MOMENT) < OutIn.Login, CONVERT(decimal(10,2), DATEDIFF(second ,Shrink.START_MOMENT,OutIn.Login)/60.0) ,0.00) AS Late,
ISNULL(IIF(Convert(decimal(10,2), DATEDIFF(second, Shrink.STOP_MOMENT, OutIn.logout)/60.0) = NULL OR Convert(decimal(10,2), DATEDIFF(second, Shrink.STOP_MOMENT, OutIn.logout)/60.0) > 0.00, 0.00, ABS(Convert(decimal(10,2), DATEDIFF(second, Shrink.STOP_MOMENT, OutIn.logout)/60.0))), 0.00) AS [Left],
IIF(OutIn.login IS NULL AND Shrink.SEG_CODE = 'SHIFT' ,DATEDIFF(minute,Shrink.START_MOMENT, Shrink.STOP_MOMENT), 0.00) AS NCNS
FROM (Epi Right JOIN Shrink on Epi.EMP_ID = Shrink.EMP_ID) INNER JOIN OutIn ON (Shrink.DATE = OutIn.DATE) JOIN (
WHERE Shrink.SEG_CODE = 'PABS' OR Shrink.SEG_CODE = 'UABS' OR Shrink.SEG_CODE = 'PPBA' OR Shrink.SEG_CODE = 'UPBA' OR Shrink.SEG_CODE = 'VACA' OR Shrink.SEG_CODE = 'JURY' OR Shrink.SEG_CODE = 'GOVT' OR Shrink.SEG_CODE = 'LOA' OR Shrink.SEG_CODE = 'BRVMNT' OR Shrink.SEG_CODE = 'FH' or Shrink.SEG_CODE = 'SHIFT'
ORDER BY Badge;
Sorry its kind of messy. Again, Thank you
EDIT2
Per request, here is some of my data:
Shrink:
EMP_SK EMP_ID EMP_LAST_NAME EMP_FIRST_NAME EMP_SORT_NAME EMP_SHORT_NAME EMP_SENIORITY EMP_EFF_HIRE_DATE NOM_DATE SEG_CODE START_MOMENT STOP_MOMENT
-9.88181E+11 73485 BLAH BLAH BLAH BLAH 20130812000 8/12/2013 0:00:00 10/6/2013 0:00:00 SHIFT 10/6/2013 12:00:00 10/6/2013 21:00:00
-9.88181E+11 73485 BLAH BLAH BLAH BLAH 20130812000 8/12/2013 0:00:00 10/7/2013 0:00:00 SHIFT 10/7/2013 12:00:00 10/7/2013 21:00:00
-9.88768E+11 192329 BLAH BLAH BLAH BLAH 20130715000 7/15/2013 0:00:00 10/7/2013 0:00:00 SHIFT 10/7/2013 6:00:00 10/7/2013 15:00:00
-9.88741E+11 224579 BLAH BLAH BLAH BLAH 20091214000 12/14/2009 0:00:00 10/7/2013 0:00:00 SHIFT 10/7/2013 8:00:00 10/7/2013 17:00:00
-9.88741E+11 224579 BLAH BLAH BLAH BLAH 20091214000 12/14/2009 0:00:00 10/8/2013 0:00:00 SHIFT 10/8/2013 8:00:00 10/8/2013 17:00:00
-9.88181E+11 73485 BLAH BLAH BLAH BLAH 20130812000 8/12/2013 0:00:00 10/8/2013 0:00:00 SHIFT 10/8/2013 12:00:00 10/8/2013 21:00:00
-9.88768E+11 192329 BLAH BLAH BLAH BLAH 20130715000 7/15/2013 0:00:00 10/8/2013 0:00:00 SHIFT 10/8/2013 6:00:00 10/8/2013 15:00:00
Epi:
Badge Name NT Login Email MBadge Manager Vendor Business Unit Business Unit Desc Sub-Support Name Segment Location Subqueue Phone Queue Title EWFMDeptCode EWFMDeptName EWFMTeamCode EWFMTeamName ACD/Extension Switch Name DomsID DomsID2 DomsID3 Tech-ID (DPS) Tech-ID2 (DPS2) KanaUserID NetAgentID Part_Time Alias Miscellaneous Hiredate Cost Center Jack Building QueueStatus Training Class Trainer TeamCodeName TeamCodeAbbr
73485 blah blah blah 315413 blah blah blah Service Desk Delivery blah Tech Support blah blah Client Specialty Queue Phone Agent blah blah Not Assigned Not Assigned 4341776 AC03 NULL NULL NULL NULL NULL NULL NULL 0 NULL NULL 00:00.0 blah blah blah Normal Generic Class Instructor Not Assigned Not Assigned
224579 blah blah blah 626985 blah blah blah Service Desk Delivery blah Tech Support blah blah Client Specialty Queue Phone Agent blah blah Not Assigned Not Assigned 4341991 AC03 NULL NULL NULL 211212 NULL 0 0 0 NULL NULL 00:00.0 blah blah blah Normal Generic Class Instructor Not Assigned Not Assigned
192329 blah blah blah 364970 blah blah blah Service Desk Delivery blah Tech Support blah blah Client Specialty Queue Phone Agent blah blah Not Assigned Not Assigned 4341937 AC03 NULL NULL NULL NULL NULL 0 0 0 NULL NULL 00:00.0 blah blah blah Normal Generic Class Instructor Not Assigned Not Assigned
OutIn:
row_date logid login logout
10/6/2013 0:00:00 4341776 10/6/2013 12:00:15 10/6/2013 21:00:26
10/7/2013 0:00:00 4341937 10/7/2013 6:04:48 10/7/2013 15:15:22
10/7/2013 0:00:00 4341991 10/7/2013 7:54:34 10/7/2013 17:00:39
10/7/2013 0:00:00 4341776 10/7/2013 12:00:16 10/7/2013 21:20:36
10/8/2013 0:00:00 4341937 10/8/2013 5:59:47 10/8/2013 15:01:31
10/8/2013 0:00:00 4341991 10/8/2013 7:58:46 10/8/2013 17:03:26
10/8/2013 0:00:00 4341776 10/8/2013 12:00:10 10/8/2013 14:32:20
10/8/2013 0:00:00 4341776 10/8/2013 14:32:20 10/8/2013 21:00:04
Thank you again
Final EDIT
I finally figured it out. Here is the completed code:
SELECT
a.NOM_DATE AS RowDate,
a.EMP_ID AS Badge,
ISNULL(a.[Extension], a.logid) AS [Extension],
a.Name,
a.Manager,
a.DeptName,
a.Location,
a.START_MOMENT AS ShiftStart,
a.STOP_MOMENT AS ShiftStop,
a.login AS Login,
a.logout AS Logout,
a.SEG_CODE AS OUTSEG,
IIF(a.SEG_CODE = 'PABS' OR a.SEG_CODE = 'UABS' OR a.SEG_CODE = 'PPBA' OR a.SEG_CODE = 'UPBA' OR a.SEG_CODE = 'VACA' OR a.SEG_CODE = 'JURY' OR a.SEG_CODE = 'GOVT' OR a.SEG_CODE = 'LOA' OR a.SEG_CODE = 'BRVMNT' OR a.SEG_CODE = 'FH', a.START_MOMENT, NUll) AS OutStart,
IIF(a.SEG_CODE = 'PABS' OR a.SEG_CODE = 'UABS' OR a.SEG_CODE = 'PPBA' OR a.SEG_CODE = 'UPBA' OR a.SEG_CODE = 'VACA' OR a.SEG_CODE = 'JURY' OR a.SEG_CODE = 'GOVT' OR a.SEG_CODE = 'LOA' OR a.SEG_CODE = 'BRVMNT' OR a.SEG_CODE = 'FH', a.STOP_MOMENT, NUll) AS OutStop,
IIF(DateADD(minute, 5, a.START_MOMENT) < a.Login, CONVERT(decimal(10,2), DATEDIFF(second ,a.START_MOMENT,a.Login)/60.0) ,0.00) AS Late,
ISNULL(IIF(Convert(decimal(10,2), DATEDIFF(second, a.STOP_MOMENT, a.logout)/60.0) = NULL OR Convert(decimal(10,2), DATEDIFF(second, a.STOP_MOMENT, a.logout)/60.0) > 0.00, 0.00, ABS(Convert(decimal(10,2), DATEDIFF(second, a.STOP_MOMENT, a.logout)/60.0))), 0.00) AS [Left],
IIF(a.login IS NULL AND a.SEG_CODE = 'SHIFT' ,DATEDIFF(minute,a.START_MOMENT, a.STOP_MOMENT), 0.00) AS NCNS
FROM (SELECT l.row_date, l.logid, l.[login], l.logout, f.NOM_DATE, f.EMP_ID, f.START_MOMENT, f.STOP_MOMENT, f.SEG_CODE,
f.Badge, f.Extension, f.Name, f.Manager, f.Deptname, f.Location FROM LogInOutActual l
right Join
(SELECT NOM_DATE, RTRIM(LTRIM(EMP_ID)) as EMP_ID, START_MOMENT, STOP_MOMENT, SEG_CODE, Badge,
Extension, Name, Manager, Deptname, Location FROM Shrink_Raw
LEFT OUTER JOIN Epicenter ON Shrink_Raw.EMP_ID = Epicenter.Badge
WHERE Shrink_Raw.SEG_CODE = 'PABS' OR Shrink_Raw.SEG_CODE = 'UABS' OR Shrink_Raw.SEG_CODE = 'PPBA'
OR Shrink_Raw.SEG_CODE = 'UPBA' OR Shrink_Raw.SEG_CODE = 'VACA' OR Shrink_Raw.SEG_CODE = 'JURY'
OR Shrink_Raw.SEG_CODE = 'GOVT' OR Shrink_Raw.SEG_CODE = 'LOA' OR Shrink_Raw.SEG_CODE = 'BRVMNT'
OR Shrink_Raw.SEG_CODE = 'FH' or Shrink_Raw.SEG_CODE = 'SHIFT') as f
on l.row_date = f.NOM_DATE AND l.logid = f.[ACD/Extension]) as a
WHERE Extension IS NOT NULL AND Name IS NOT NULL AND Manager IS NOT NULL and DeptName IS NOT NULL AND Location IS NOT NULL
ORDER BY RowDate, Badge;
After sleeping on it, I realized what to do. I did one join in a subquery and then another join in another subquery. It was so simple I feel stupid for posting it. Thank you to everyone that help the thinking process!