Pregunta

I have two tables for my process instrument data, First table has the Machine ID, Tripping Time, Tripping reason

Machine ID  Trip Time              Trip Reason
XA-065      03-20-2014 09:40:098    ANY
XA-065      03-24-2014 18:33:040    ANY
XA-765      03-23-2014 22:16:002    ANY
XA-070      03-21-2014 15:17:023    ANY

Second table has the Machine ID , Starting time.

Machine ID  Start Time             Operator
XA-065      03-20-2014 12:40:098    ANY
XA-065      03-24-2014 20:33:040    ANY
XA-765      03-23-2014 23:16:002    ANY
XA-070      03-21-2014 18:17:023    ANY

I’ve to join the two tables, So, for each machine I can get Machine ID,Trip Time, Start Time, and then I can add a calculated column to get the downtime "Start Time-Trip Time" The problem is, when the same machine starts and trips several times, JOIN operation matches all possible Trip/Start combinations. Which results in wrong downtime calculations. This is the result of My Join, notice what happens with Machine XA-065 :

Machine ID  Trip Time               Start Time              Downtime
XA-065      03-20-2014 09:40:098    03-20-2014 12:40:098    3 Hours
XA-065      03-20-2014 09:40:098    03-24-2014 20:33:040    **11 hours**
XA-065      03-24-2014 18:33:040    03-20-2014 12:40:098    **-6 Hours**
XA-065      03-24-2014 18:33:040    03-24-2014 20:33:040    2 Hours
XA-765      03-23-2014 22:16:002    03-23-2014 23:16:002    1 Hour
XA-070      03-21-2014 15:17:023    03-21-2014 18:17:023    3 Hours

Because JOIN will take all possible combinations for same Machine ID, I’m getting wrong data,11 Hours, -6 hours for downtime "Second and Third Rows". How can I filter JOIN operation in order to get rid of this ? I sorted the two tables in a descending orders, so, the right values come first, but still I’m getting wrong JOINED rows.

Your help is highly appreciated. A.A

¿Fue útil?

Solución

Try this. but the results can really differ if you have only one missing record in either of the tables.

Query

SELECT A.MachineID
      ,A.TripTime
      ,B.StartTime
      ,DATEDIFF(HOUR, A.TripTime,B.StartTime) AS DownTime
FROM 
  (
  SELECT * 
    ,ROW_NUMBER() OVER (PARTITION BY MachineID ORDER BY TripTime ASC) AS RN
  FROM dbo.MachineTrip
  ) A
      INNER JOIN
  (
   SELECT * 
       ,ROW_NUMBER() OVER (PARTITION BY MachineID ORDER BY StartTime ASC) AS RN
   FROM dbo.MachineStart
   )B
ON A.MachineID = B.MachineID AND A.RN = B.RN

Result Set

╔═══════════╦═════════════════════════╦═════════════════════════╦══════════╗
║ MachineID ║        TripTime         ║        StartTime        ║ DownTime ║
╠═══════════╬═════════════════════════╬═════════════════════════╬══════════╣
║ XA-065    ║ 2014-03-20 09:40:00.097 ║ 2014-03-20 12:40:00.097 ║        3 ║
║ XA-065    ║ 2014-03-24 18:33:00.040 ║ 2014-03-24 20:33:00.040 ║        2 ║
║ XA-070    ║ 2014-03-21 15:17:00.023 ║ 2014-03-21 18:17:00.023 ║        3 ║
║ XA-765    ║ 2014-03-23 22:16:00.003 ║ 2014-03-23 23:16:00.003 ║        1 ║
╚═══════════╩═════════════════════════╩═════════════════════════╩══════════╝

Working SQL FIDDLE

Suggestion

Your schema needs some serious attention. You should really have a foreign key constraint between these two tables which binds a record in one table to a record in other table.

OR

You can have all the records in one table with a column which binds two related rows together and a column (Maybe a bit column) which indicates whether a record is starttime or triptime.

Your schema as it is very much error prone.

Otros consejos

You can also do this with join and aggregation. This is a bit more robust if you have missing records or multiple start records in a row.

select mt.MachineId, mt.TripTime, min(ms.StartTime) as StartTime,
       datediff(hour, mt.TripTime, min(ms.StartTime)) as HoursDifference
from MachineTrip mt join
     MachineStart ms
     on mt.MachineId = ms.MachineId and
        mt.TripTime < ms.StartTime
group by mt.MachineId, mt.TripTime;

Note I used the same naming conventions as Ali so you can try it on that SQL Fiddle.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top