August 26, 2008 at 6:38 am
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.
August 26, 2008 at 6:55 am
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]
August 26, 2008 at 7:15 am
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.
August 26, 2008 at 7:35 am
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]
August 26, 2008 at 8:07 am
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]
August 26, 2008 at 8:10 am
with MyCTE as
( select
empno
, datediff(d, startdate, enddate) 'worktime'
from test
)
select empno, sum(worktime) 'total'
from MyCTE
group by empno
August 26, 2008 at 8:18 am
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]
August 26, 2008 at 8:29 am
Sorry about that; should have noticed the forum 🙁
August 26, 2008 at 11:38 am
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