I am curious if one of you guys can help me to calculate how much days (DAYS BETWEEN TravelStart
AND TravelEnd
) a certain boat BoatID
has been used ONLY for luxury tours BoutTourID = Luxury
by different captains CaptainID
,
and now for the weird part: UNTIL the next Standard tour BoutTourID = Standard
starts. I don't want to take the Cancelled trips into account Status = Cancelled
.
CaptainID BoatID BoatTourID Status TravelStart TravelEnd
Jack AlphaBoat Standard 1-7-2019 20-7-2019
Kevin AlphaBoat Luxury 21-7-2019 31-7-2019
Eric AlphaBoat Luxury Cancelled 1-8-2019 10-8-2019
Nick AlphaBoat Standard 11-8-2019 20-8-2019
John AlphaBoat Luxury 21-8-2019 30-8-2019
Lionel BigBoat Standard 1-8-2019 20-8-2019
Jeffrey BigBoat Luxury 20-8-2019 25-8-2019
Chris BigBoat Standard 26-8-2019 28-8-2019
This in SQL should give the following results, so in the basis the table shows the exact same amount of records:
CaptainID
Jack 0 --since BoatTourID = Standard, it should not be calculated
Kevin 10
Eric 0 --since Status = Cancelled
Nick 0
John 9
Lionel 19
Jeffrey 5
Chris 2
It should be possible to run it in 1 SQL query.
The code I wrote so far is very messy and doesn't come close to solving it, so I rather not post it, since I hope for a fresh idea. In case I will still post it, if necessary!