I have a large database with connections between cities. Each connection has a start and destination town, a start date, and a price for that connection.
I'd like to compute any combinations of outgoing+return connections, for any connections, and for dates where the return connection is between 1-20 days. Then select the best price for each date combination.
Example:
Table:
city_start, city_end, date_start, price
Hamburg Berlin 01.01.2016 100.00
Berlin Hamburg 10.01.2016 112.00
Berlin Hamburg 10.01.2016 70.00
Berlin Hamburg 12.01.2016 50.00
Berlin Hamburg 30.02.2016 20.00
Paris Madrid ...
Madrid Paris
London Paris
Desired result:
Hamburg-Berlin-Hamburg, 01.01.2016, 10.01.2016, 170.00 (100+70)
Hamburg-Berlin-Hamburg, 01.01.2016, 12.01.2016, 150.00 (100+50)
...
(not Berlin-Hamburg on 30.02.2016 because it's >20 days from departure drive)
(not London-Paris, as there is no return Paris-London)
I can get the possible combinations by:
SELECT DISTINCT city_start, city_end, city_end, city_start from table
But how can I now compute their permutations?