Clock-In/ Clock-Out

  • dwain.c (2/11/2013)


    No idea because I don't know what Day 01, Day 02, etc. are supposed to represent.

    ;WITH Punches AS (

    SELECT *

    ,rn=(ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY punchTime)-1)/2

    FROM @table)

    SELECT EmployeeID

    ,[Full_name] = MAX([Full_name])

    ,ClockIN=MIN(punchTime)

    ,ClockOUT=MAX(punchTime)

    ,date_start=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,date_end=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,day_name=DATEPART(dw,MIN(punchTime))

    ,Hrs=DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60.

    ,[day_value]= (SELECT day_value FROM TAT_Shiftpatterns WHERE day_name = 'Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar) and Employee_number = '778' )

    ,[work_pattern] = MAX([work_pattern])

    ,[Hrs_diff] =DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60. - (SELECT day_value FROM TAT_Shiftpatterns WHERE day_name = 'Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar) and Employee_number = '778' )

    FROM Punches a

    INNER JOIN [TAT_Shiftpatterns] b ON b.Employee_number = a.EmployeeID

    GROUP BY EmployeeID, rn

    by running the above code i am getting the expected output, but i have to hard code the employee number. Is there any alternative to this approach.

    Thanks in advance

  • p.ramchander (2/11/2013)


    dwain.c (2/11/2013)


    No idea because I don't know what Day 01, Day 02, etc. are supposed to represent.

    ;WITH Punches AS (

    SELECT *

    ,rn=(ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY punchTime)-1)/2

    FROM @table)

    SELECT EmployeeID

    ,[Full_name] = MAX([Full_name])

    ,ClockIN=MIN(punchTime)

    ,ClockOUT=MAX(punchTime)

    ,date_start=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,date_end=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,day_name=DATEPART(dw,MIN(punchTime))

    ,Hrs=DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60.

    ,[day_value]= (SELECT day_value FROM TAT_Shiftpatterns WHERE day_name = 'Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar) and Employee_number = '778' )

    ,[work_pattern] = MAX([work_pattern])

    ,[Hrs_diff] =DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60. - (SELECT day_value FROM TAT_Shiftpatterns WHERE day_name = 'Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar) and Employee_number = '778' )

    FROM Punches a

    INNER JOIN [TAT_Shiftpatterns] b ON b.Employee_number = a.EmployeeID

    GROUP BY EmployeeID, rn

    by running the above code i am getting the expected output, but i have to hard code the employee number. Is there any alternative to this approach.

    Thanks in advance

    I think you need to read up a bit on GROUP BY. This is what I would do:

    ;WITH Punches AS (

    SELECT *

    ,rn=(ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY punchTime)-1)/2

    FROM @table)

    SELECT EmployeeID

    ,[Full_name] = MAX([Full_name])

    ,ClockIN=MIN(punchTime)

    ,ClockOUT=MAX(punchTime)

    ,date_start=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,date_end=CONVERT(VARCHAR(10),MIN(punchtime),111)

    ,day_name=DATEPART(dw,MIN(punchTime))

    ,Hrs=DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60.

    ,[day_value]='Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar)

    ,[work_pattern] = MAX([work_pattern])

    ,[Hrs_diff] =DATEDIFF(minute,MIN(punchTime), MAX(punchTime))/60. - (SELECT day_value FROM TAT_Shiftpatterns WHERE day_name = 'Day 0' + CAST(DATEPART(dw,MIN(punchTime)) AS varchar) and Employee_number = '778' )

    FROM Punches a

    INNER JOIN [TAT_Shiftpatterns] b ON b.Employee_number = a.EmployeeID

    GROUP BY EmployeeID, rn

    Not tested but it should work.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 2 posts - 16 through 16 (of 16 total)

You must be logged in to reply to this topic. Login to reply