Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sum of time per month

sum of time per month

Scheduled Pinned Locked Moved Database
database
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hunain Hafeez
    wrote on last edited by
    #1

    This query shows emplName,emplID, totalworking time, InTime, OutTime, DateVisited, Overtime for an employee based on his InTime and Outime, that's OK. Now i am trying to modify it to show only emplID, EmplName, Total Working hours(Per month), total overtime (per month). e.g.

    Empid EmplName TotalWorkingHours TotalOvertime Month
    00001 John 77:00 05:55 2013-02
    00002 Masn 57:00 04:56 2013-02

    Query:

    with times as (
    SELECT t1.EmplID
    , t3.EmplName
    , min(t1.RecTime) AS InTime
    , max(t2.RecTime) AS [TimeOut]
    , t4.ShiftId as ShiftID
    , t4.StAtdTime as ShStartTime
    , t4.EndAtdTime as ShEndTime
    , cast(min(t1.RecTime) as datetime) AS InTimeSub
    , cast(max(t2.RecTime) as datetime) AS TimeOutSub
    , t1.RecDate AS [DateVisited]
    FROM AtdRecord t1
    INNER JOIN
    AtdRecord t2
    ON t1.EmplID = t2.EmplID
    AND t1.RecDate = t2.RecDate
    AND t1.RecTime < t2.RecTime
    inner join
    HrEmployee t3
    ON t3.EmplID = t1.EmplID
    inner join AtdShiftSect t4
    ON t3.ShiftId = t4.ShiftId
    group by
    t1.EmplID
    , t3.EmplName
    , t1.RecDate
    , t4.ShiftId
    , t4.StAtdTime
    , t4.EndAtdTime
    )
    SELECT
    EmplID
    ,EmplName
    ,ShiftId As ShiftID
    ,InTime
    ,[TimeOut]
    ,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) TotalWorkingTime
    ,[DateVisited]
    ,CASE WHEN [InTime] IS NOT NULL AND [TimeOut] IS NOT NULL THEN
    CONVERT(char(5),CASE WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S002' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),CAST([TimeOutSub] AS DATETIME)),0), 108),5)
    WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S001' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME), CAST([TimeOutSub] AS DATETIME)),0), 108),5)
    ELSE '00:00' END, 108)
    ELSE 'ABSENT' END AS OverTime
    FROM times order by EmplID, ShiftID, DateVisited

    L K 2 Replies Last reply
    0
    • H Hunain Hafeez

      This query shows emplName,emplID, totalworking time, InTime, OutTime, DateVisited, Overtime for an employee based on his InTime and Outime, that's OK. Now i am trying to modify it to show only emplID, EmplName, Total Working hours(Per month), total overtime (per month). e.g.

      Empid EmplName TotalWorkingHours TotalOvertime Month
      00001 John 77:00 05:55 2013-02
      00002 Masn 57:00 04:56 2013-02

      Query:

      with times as (
      SELECT t1.EmplID
      , t3.EmplName
      , min(t1.RecTime) AS InTime
      , max(t2.RecTime) AS [TimeOut]
      , t4.ShiftId as ShiftID
      , t4.StAtdTime as ShStartTime
      , t4.EndAtdTime as ShEndTime
      , cast(min(t1.RecTime) as datetime) AS InTimeSub
      , cast(max(t2.RecTime) as datetime) AS TimeOutSub
      , t1.RecDate AS [DateVisited]
      FROM AtdRecord t1
      INNER JOIN
      AtdRecord t2
      ON t1.EmplID = t2.EmplID
      AND t1.RecDate = t2.RecDate
      AND t1.RecTime < t2.RecTime
      inner join
      HrEmployee t3
      ON t3.EmplID = t1.EmplID
      inner join AtdShiftSect t4
      ON t3.ShiftId = t4.ShiftId
      group by
      t1.EmplID
      , t3.EmplName
      , t1.RecDate
      , t4.ShiftId
      , t4.StAtdTime
      , t4.EndAtdTime
      )
      SELECT
      EmplID
      ,EmplName
      ,ShiftId As ShiftID
      ,InTime
      ,[TimeOut]
      ,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) TotalWorkingTime
      ,[DateVisited]
      ,CASE WHEN [InTime] IS NOT NULL AND [TimeOut] IS NOT NULL THEN
      CONVERT(char(5),CASE WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S002' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),CAST([TimeOutSub] AS DATETIME)),0), 108),5)
      WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S001' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME), CAST([TimeOutSub] AS DATETIME)),0), 108),5)
      ELSE '00:00' END, 108)
      ELSE 'ABSENT' END AS OverTime
      FROM times order by EmplID, ShiftID, DateVisited

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just remove the columns you don't need and you should be good to go. Are you facing any issues?

      H 1 Reply Last reply
      0
      • L Lost User

        Just remove the columns you don't need and you should be good to go. Are you facing any issues?

        H Offline
        H Offline
        Hunain Hafeez
        wrote on last edited by
        #3

        sorry what ? i need sum of time per month sir

        1 Reply Last reply
        0
        • H Hunain Hafeez

          This query shows emplName,emplID, totalworking time, InTime, OutTime, DateVisited, Overtime for an employee based on his InTime and Outime, that's OK. Now i am trying to modify it to show only emplID, EmplName, Total Working hours(Per month), total overtime (per month). e.g.

          Empid EmplName TotalWorkingHours TotalOvertime Month
          00001 John 77:00 05:55 2013-02
          00002 Masn 57:00 04:56 2013-02

          Query:

          with times as (
          SELECT t1.EmplID
          , t3.EmplName
          , min(t1.RecTime) AS InTime
          , max(t2.RecTime) AS [TimeOut]
          , t4.ShiftId as ShiftID
          , t4.StAtdTime as ShStartTime
          , t4.EndAtdTime as ShEndTime
          , cast(min(t1.RecTime) as datetime) AS InTimeSub
          , cast(max(t2.RecTime) as datetime) AS TimeOutSub
          , t1.RecDate AS [DateVisited]
          FROM AtdRecord t1
          INNER JOIN
          AtdRecord t2
          ON t1.EmplID = t2.EmplID
          AND t1.RecDate = t2.RecDate
          AND t1.RecTime < t2.RecTime
          inner join
          HrEmployee t3
          ON t3.EmplID = t1.EmplID
          inner join AtdShiftSect t4
          ON t3.ShiftId = t4.ShiftId
          group by
          t1.EmplID
          , t3.EmplName
          , t1.RecDate
          , t4.ShiftId
          , t4.StAtdTime
          , t4.EndAtdTime
          )
          SELECT
          EmplID
          ,EmplName
          ,ShiftId As ShiftID
          ,InTime
          ,[TimeOut]
          ,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) TotalWorkingTime
          ,[DateVisited]
          ,CASE WHEN [InTime] IS NOT NULL AND [TimeOut] IS NOT NULL THEN
          CONVERT(char(5),CASE WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S002' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),CAST([TimeOutSub] AS DATETIME)),0), 108),5)
          WHEN CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S001' Then LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME), CAST([TimeOutSub] AS DATETIME)),0), 108),5)
          ELSE '00:00' END, 108)
          ELSE 'ABSENT' END AS OverTime
          FROM times order by EmplID, ShiftID, DateVisited

          K Offline
          K Offline
          King Fisher
          wrote on last edited by
          #4

          :thumbsup: refer this link for adding time

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups