1

If I have a data set with a composite key of an order number and a timestamp, can I iterate through the timestamps to search for data values at the specific time? For example, I would want to iterate through the specific order number until I find the first timestamp that contains a BOL and then insert that date record into a new table. In general I am trying to find days between events in the "life-cycle" of an order.

Example Data:

| Order | Timestamp        | BOL     |
| ------| ----------       |---------|
| 12345 | 05.25.21.05:00   |null     |
| 12345 | 05.26.21.05:00   |512345   |
| 67890 | 05.25.21.05:00   |null     |
| 67890 | 05.26.21.05:00   |556725   |

Pseudocode:

SET @i=0;   
while(i<size(order.timestamps())){  
    if(order.timestamp[i].BOL is NULL)
        i++;
    else
        order.bolDate = order.timestamp(i);
        exit;
}

Thank you for any assistance even if it's nothing more than a link!

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786

1 Answers1

0

well, seems like you want to to this , this way you don't need to iterate :

insert into newtable (<column list>)
select  order , timestamp , bop 
from ( select * , row_number() over (partition by Order order by timestamp) rn 
 from dataTable
where BOP is not null 
) t 
where rn = 1

query above finds the earliest timestamp for each Order that has BOP and directly can data be inserted into new table.

eshirvana
  • 23,227
  • 3
  • 22
  • 38