Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Query Help

Author  Topic 

teknomec
Starting Member

4 Posts

Posted - 2012-02-16 : 04:03:59
I have table values like below.

JOBNUMBER ACTIVITYTIME STATUS ENGINEER
1789 2011-12-27 14:56:29.000 Left for site 50
1789 2011-12-27 14:56:42.000 Reached the site 50
1789 2011-12-27 15:07:46.000 Work started 50
1789 2012-01-11 12:29:11.000 Held for Other Reasons 50

I need to calculate values like below

Jobnumber Traveltime(Activitytime) Worktime Employee
1789 Reached the site - Left for site Workstarted - Held for Other Reasons 50

To calculate traveltime i need to subtract Acivitytime between Left for site and Reached the site.

To calculate work time i need to subtract Activity time between Workstarted and held for other reasons

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-02-16 : 04:10:22
The Status is in text or do you have a status code ?



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

teknomec
Starting Member

4 Posts

Posted - 2012-02-16 : 04:13:37
It is a standard text.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-02-16 : 04:33:49
[code]
select JOBNUMBER,
Traveltime = convert(varchar(10), dateadd(second, datediff(second,
min(case when STATUS = 'Left for site' then ACTIVITYTIME end),
min(case when STATUS = 'Reached the site' then ACTIVITYTIME end)
), 0), 108),
Worktime = convert(varchar(10), dateadd(second, datediff(second,
min(case when STATUS = 'Work started' then ACTIVITYTIME end),
min(case when STATUS = 'Held for Other Reasons' then ACTIVITYTIME end)
), 0), 108),
Employee = ENGINEER
from yourtable
group by JOBNUMBER, ENGINEER
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

teknomec
Starting Member

4 Posts

Posted - 2012-02-16 : 05:14:48
Thanks for the reply. when i run the query i get null values like.

1789 NULL NULL 50
1795 NULL NULL 50

should i do any other modification in the query.
Go to Top of Page
   

- Advertisement -