Home Forums SQL Server 2008 T-SQL (SS2K8) SQL - Need to add a flag when certain conditions are met RE: SQL - Need to add a flag when certain conditions are met

  • Okay, take a look at the following code:

    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'

    ),

    LEAVES_TOGETHER AS (

    SELECT D.*

    FROM SampleData AS D

    INNER JOIN SampleData AS S

    ON D.PERSON = S.PERSON

    AND D.TRANSACTDATE = S.TRANSACTDATE

    AND D.PAYCODE <> S.PAYCODE

    AND S.PAYCODE IN ('LEAVE1', 'LEAVE2')

    WHERE D.PAYCODE IN ('LEAVE1', 'LEAVE2')

    )

    SELECT SD.PERSON

    , SD.TransactDate, SD.STARTDATE, SD.END_DATE

    , MIN(SD.IN_PUNCH) OVER (PARTITION BY SD.Person, SD.TransactDate) AS IN_PUNCH

    , MAX(SD.OUT_PUNCH) OVER (PARTITION BY SD.Person, SD.TransactDate) AS OUT_PUNCH

    , SD.[HOURS]

    , SD.PAYCODE

    , RIGHT(LT.PAYCODE, 1) AS FLAG

    FROM SampleData AS SD

    LEFT OUTER JOIN LEAVES_TOGETHER AS LT

    ON SD.PERSON = LT.PERSON

    AND SD.TRANSACTDATE = LT.TRANSACTDATE

    AND SD.STARTDATE = LT.STARTDATE

    AND SD.END_DATE = LT.END_DATE

    AND SD.[HOURS] = LT.[HOURS]

    AND SD.IN_PUNCH = LT.IN_PUNCH

    AND SD.OUT_PUNCH = LT.OUT_PUNCH

    ORDER BY SD.PERSON, SD.TRANSACTDATE, SD.PAYCODE;

    I'm not sure how this will perform, as a look at the execution plan shows that 98% of the cost is in two Sort operators. This may mean that a properly indexed source table would allow this to fly, but only testing can determine that. Sizable volume could also slow it down. Let me know...

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)