How to get total time for each employee

  • I have a table like

    CREATE TABLE [dbo].[test](

    [empno] [varchar](20) NOT NULL,[startdate] [datetime] NULL,

    [enddate] [datetime] NULL)

    and it has values like

    insert into test

    select '1-72-0143','2002-11-12 00:00:00.000','2003-06-27 00:00:00.000'

    union all

    select '1-72-0143','2003-06-27 00:00:00.000','2003-08-15 00:00:00.000'

    union all

    select '1-72-0143','2003-08-15 00:00:00.000','2003-12-30 00:00:00.000'

    union all

    select '1-72-0143','2004-05-13 00:00:00.000','2006-04-29 00:00:00.000'

    union all

    select '1-72-0143','2006-04-29 00:00:00.000','2008-05-12 00:00:00.000'

    union all

    select '127-1986-1','2002-04-18 00:00:00.000','2002-08-28 00:00:00.000'

    union all

    select '127-1986-1','2002-08-29 00:00:00.000','2006-11-18 00:00:00.000'

    union all

    select '127-1986-1','2006-11-18 00:00:00.000','2007-09-03 00:00:00.000'

    There are two employees with their posting start and end dates. How could i get the whole time of posting (enddate-startdate) for each employee.

    Regards

    DBDigger Microsoft Data Platform Consultancy.

  • HI there,

    Is this what you looking for?

    SELECT

    s.[empno] ,

    DATEDIFF(minute,s.[startdate],e.[enddate])

    ,[startdate]

    ,[enddate]

    FROM (SELECT MAX(Enddate) as Enddate, [empno] FROM test GROUP BY [empno]) e

    INNER JOIN (SELECT MIN(Startdate) as Startdate , [empno] FROM test GROUP BY [empno]) s

    ON s.[empno] = e.[empno]

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Is it time for each section added or time from first to last timestamp for each employee?

    You might need to built a CTE to grab all the times and then sum those up and group by employee, depending on your needs.

  • i require time for each section added. Because there may be gaps between the pairs of dates.

    DBDigger Microsoft Data Platform Consultancy.

  • Hi again.

    Could you give us an example of what the ouput should be?

    Thanks

    Chris

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Here is the way i want to calculate the dates for each employee

    .

    At the end i will have X.Y year as result. I require accuracy till months only.

    DBDigger Microsoft Data Platform Consultancy.

  • WOuld this be what you want?

    SELECT

    [empno],

    SUM(DATEDIFF(Month,[startdate],[enddate])) as tot

    FROM test

    GROUP BY [empno]

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • with MyCTE as

    ( select

    empno

    , datediff(d, startdate, enddate) 'worktime'

    from test

    )

    select empno, sum(worktime) 'total'

    from MyCTE

    group by empno

  • I think calculation is no correct. It could not be 61 (years) from 2002 to 2008. We have to divide the datediff with 12 and some thing like that.

    DBDigger Microsoft Data Platform Consultancy.

  • My Answer is in Months

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • I have my production server on SQL Server 2000. So no CTE here.:unsure:

    DBDigger Microsoft Data Platform Consultancy.

  • Ok , thanks alot christopher. i hope it will work.

    DBDigger Microsoft Data Platform Consultancy.

  • Sorry about that; should have noticed the forum 🙁

  • Its working now. and thanks for all of you.

    DBDigger Microsoft Data Platform Consultancy.

  • Thank you very much. I am doing downtime reporting and this worked great. I just changed months to minutes to get the minutes from my DT_Start, DT_Stop.

    I do the Controls Automation and have my own SQL Servers and this stuff is all new to me but with resources like this I am learning quickly.

Viewing 15 posts - 1 through 14 (of 14 total)

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