我有一个包含城市之间连接的大型数据库。每个连接都有一个开始和目的地城镇、一个开始日期以及该连接的价格。
我想为任何连接以及返回连接在 1-20 天之间的日期计算传出+返回连接的任何组合。然后为每个日期组合选择最优惠的价格。
例子:
表:
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
期望的结果:
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)
我可以通过以下方式获得可能的组合:
SELECT DISTINCT city_start, city_end, city_end, city_start from table
但是我现在如何计算它们的排列?
最佳答案
获取所有对的查询使用 join
:
select tto.city_start, tto.city_end, tto.date_start, tfrom.date_end,
(tto.price + tfrom.price) as price
from t tto join
t tfrom
on tto.city_end = tfrom.city_start and
tto.city_start = tfrom.city_end and
tfrom.date_start >= tto.date_start + interval '1 day' and
tfrom.date_end <= tto.date_start + interval '20 day';
要获得最便宜的价格,请使用窗口函数:
select tt.*
from (select tto.city_start, tto.city_end, tto.date_start, tfrom.date_end,
(tto.price + tfrom.price) as price,
row_number() over (partition by tto.city_start, tto.city_end order by (tto.price + tfrom.price) asc) as seqnum
from t tto join
t tfrom
on tto.city_end = tfrom.city_start and
tto.city_start = tfrom.city_end and
tfrom.date_start >= tto.date_start + interval '1 day' and
tfrom.date_end <= tto.date_start + interval '20 day'
) tt
where seqnum = 1;
关于sql - 如何使用 postgresql 计算排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36621613/