Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Select by Max(predictiondate) and by Days Difference in a single Sql transaction

$
0
0

Imagine I have a Truck to transport goods and give to my customers some predictions

I have  the following predictions

date of prediction   City     Day                                          
                                                          
04/25                Lisbon   1-2 May                
04/25                Lisbon   6 May                   


at day 26 I make a new prediction

date of prediction   City     Day                       
04/26                Lisbon   1 may                      

                      Database
            preddate   city    availdate
            04/25/2014  Lisbon  05/01/2014
            04/25/2014  Lisbon  05/02/2014
            04/25/2014  Lisbon  05/06/2014
            04/26/2014  Lisbon  05/01/2014
           
           
  I need a select that retrieves 2 records-  1 and 6 of May . Using max(preddate) works for these 2 days (please,
  read till the end).  My problem is how to discard 05/02/2014.
 
  I can assume that +- 2 days of difference is a correction   and more
  than that is a new cycle. I explain:
 
  My truck is at Lisbon arround 1 May, and will be again, after a tour, at 6.
  As 2 May is not in the last prediction and the difference for the 1 May is one day, I discard it.
  6 of May is not in the last prediction but it is valid because the difference of days to 1 May is more than 2 days
 
  Hope that this information is enough and if it is viable!

Thks in advance


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>