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.
| Author |
Topic |
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2010-04-14 : 07:40:56
|
| I have the below table:Gp Sq StarTime EndTime Operation1 1 10:30:00 10:31:00 A1 2 10:34:00 10:55:00 B1 3 10:43:45 10:44:45 C2 1 10:45:00 10:47:00 D2 2 10:49:00 10:59:00 E2 3 11:32:45 11:44:45 F2 4 11:35:00 11:37:00 G3 1 11:39:00 11:51:00 H3 2 11:52:45 11:59:45 JThe query result should be as below:Gp StartTime EndTime Op_Start Op_End TotalTime1 10:30:00 10:44:45 A C 00:14:451 10:45:00 11:37:00 D G 00:52:001 11:39:00 11:59:45 H J 00:20:45Thanks for any help |
|
|
Sachin.Nand
2937 Posts |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2010-04-14 : 08:06:17
|
| Thank you for the help, but The result should show the first operation and the last operation it is basically the fisrt and the last and may not be min and max. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-14 : 08:14:32
|
| [code]select Gp,min(StarTime),t3.EndTime,min(Operation)as Op_Start ,max(Operation)as Op_End,"timediff function"(min(StarTime),t3.EndTime)as TotalTime from yourtable t1cross apply( select top 1 EndTime as EndTime from yourtable t2 where t2.Gp=t1.Gp order by Sq desc )t3group by Gp,t3.EndTime[/code]PBUH |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2010-04-14 : 08:27:34
|
| Thank you so much |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2010-04-14 : 08:33:29
|
| I would like to add the below to the query resultGp StartTime EndTime Op_Start Op_End TotalTime HoldTime1 10:30:00 10:44:45 A C 00:14:45 00:00:152 10:45:00 11:37:00 D G 00:52:00 00:02:003 11:39:00 11:59:45 H J 00:20:45The last column being the difference of the EndTime of Group1 and StartTime of Group2 and so on except the last columnThanks,T |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-14 : 09:09:23
|
| [code];with cteas(select Gp,min(StarTime),t3.EndTime,min(Operation)as Op_Start ,max(Operation)as Op_End,"timediff function"(min(StarTime),t3.EndTime)as TotalTime from yourtable t1cross apply( select top 1 EndTime as EndTime from yourtable t2 where t2.Gp=t1.Gp order by Sq desc )t3group by Gp,t3.EndTime)select *,"timediff function"(t1.EndTime,t2.StartTime) from cte t1 left join cte t2 where t1.Gp=t2.Gp+1[/code]PBUH |
 |
|
|
|
|
|
|
|