I have a table where when an employee works LEAVE1 and LEAVE2 paycodes on the same day then in the new column called 'FLAG' we need to put a 1 for LEAVE1 and a 2 for LEAVE2, when these paycodes are not there on the same day then flag will be NULL as for other paycodes.
Tried using the CASE statements but it always puts a 1 or 2 for the respective paycodes regardless of the fact whether they were together on the same day or not.
Thank you and really appreciate all the help I get from this wonderful group.
Sample Table
WITH SampleData
(PERSON,TRANSACTDATE, STARTDATE, END_DATE, IN_PUNCH,OUT_PUNCH,HOURS, PAYCODE)
AS
(
SELECT 1234,'08/03/2015','08/03/2015','08/03/2015', '06:00','09:00','3', 'REG1'
UNION ALL
SELECT 1234,'08/03/2015','08/03/2015','08/03/2015', '09:00','13:00','4','REG2'
UNION ALL
SELECT 1234,'08/04/2015','08/04/2015','08/04/2015', '09:00','13:00','4','LEAVE1'
UNION ALL
SELECT 1234,'08/04/2015','08/04/2015','08/04/2015', '14:00','16:00','2', 'LEAVE2'
UNION ALL
SELECT 1234,'08/05/2015','08/05/2015','08/05/2015', '08:00','09:00','1', 'LEAVE1'
UNION ALL
SELECT 4553,'08/05/2015','08/05/2015','08/05/2015', '08:00','09:00','1', 'REG1'
UNION ALL
SELECT 4553,'08/05/2015','08/05/2015','08/05/2015', '10:00','12:00','2','LEAVE2'
)
SELECT Person
, TransactDate, STARTDATE, END_DATE
, MIN(IN_PUNCH) OVER (PARTITION BY Person,TransactDate) AS IN_PUNCH
, MAX(OUT_PUNCH) OVER (PARTITION BY Person, TransactDate) AS OUT_PUNCH
,HOURS
,PAYCODE
FROM SampleData
Current Results
Person TransactDate STARTDATE END_DATE IN_PUNCH OUT_PUNCH HOURS PAYCODE
1234 08/03/2015 08/03/2015 08/03/2015 06:00 13:00 3 REG1
1234 08/03/2015 08/03/2015 08/03/2015 06:00 13:00 4 REG2
1234 08/04/2015 08/04/2015 08/04/2015 09:00 16:00 4 LEAVE1
1234 08/04/2015 08/04/2015 08/04/2015 09:00 16:00 2 LEAVE2
1234 08/05/2015 08/05/2015 08/05/2015 08:00 09:00 1 LEAVE1
4553 08/05/2015 08/05/2015 08/05/2015 08:00 12:00 2 LEAVE2
4553 08/05/2015 08/05/2015 08/05/2015 08:00 12:00 1 REG1
Desired Results
Person TransactDate STARTDATE END_DATE IN_PUNCH OUT_PUNCH HOURS PAYCODE FLAG
1234 08/03/2015 08/03/2015 08/03/2015 06:00 13:00 3 REG1 NULL
1234 08/03/2015 08/03/2015 08/03/2015 06:00 13:00 4 REG2 NULL
1234 08/04/2015 08/04/2015 08/04/2015 09:00 16:00 4 LEAVE1 1
1234 08/04/2015 08/04/2015 08/04/2015 09:00 16:00 2 LEAVE2 2
1234 08/05/2015 08/05/2015 08/05/2015 08:00 09:00 1 LEAVE1 NULL
4553 08/05/2015 08/05/2015 08/05/2015 08:00 12:00 2 LEAVE2 NULL
4553 08/05/2015 08/05/2015 08/05/2015 08:00 12:00 1 REG1 NULL
Thank you for the quick help. Unfortunately. it's a 2008 server and I'm not familiar with OUTER APPLY
. Can a solution be posted without the OUTER APPLY
?