Combine records to calculate - David S.

29-Mar-07 11:17:32
I have a table that contains arrival times for different vehicles. I would
like to be able to combine records by incident and calculate the difference
in time of thier arrival.

Example

incd    vehi    time
123    m03    12:04
123    e05    12:07
124    m02    14:09
124    e07    14:06

This is what I want to do.

incd    vehi1    vehi2    time    diff
123    m03    e05        12:04    :03
124    m02    e07        14:09    -:03

Notice that I want to always use the "m" vehicles as the primary and
calculate the difference in time of that even if it results in a negative
time, meaning they were not the first to arrive.

Can anyone help me?

ds
reply
 
 

Combine records to calculate - Tom Cooper

29-Mar-07 11:34:25
Assuming that your time columns are of type datetime, then something like

Select incd, m.vehi as vehi1, e.vehi as vehi2, m.time, DateDiff(mi, m.time,
e.time) As diff
From yourtable m
Inner Join yourtable e On m.incd = e.incd
Where m.vehi Like 'm%' And e.vehi Like 'e%'

Tom
reply
 

Combine records to calculate - Roy Goldhammer

29-Mar-07 11:46:51
Here is another example to solve it:
CREATE TABLE #tmp(id int,
Orig varchar(3),
Arrival datetime)


insert #tmp values(123,'m03','12:04')
insert #tmp values(123,'e05','12:07')
insert #tmp values(124,'m02','14:09')
insert #tmp values(124,'e07','14:06')


select ID, max(case when left(Orig,1) = 'm' then Orig else '' end) vehi1
, max(case when left(Orig,1) = 'e' then Orig else '' end) vehi2
, max(case when left(Orig,1) = 'm' then Arrival else '' end) time
, datediff(minute, max(case when left(Orig,1) = 'm' then Arrival else ''
end),
max(case when left(Orig,1) = 'e' then Arrival else '' end)) diff
from #tmp
GROUP BY ID
reply
 
stored procedure question
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class