我必须拥有类似数据的表格。一个服务输出连接,一个服务收入。
table_out:
from_,to_,departure,price,name...
table_in:
from_,to_,depature,price,name...
我想加入这些表,以便我可以创建从一个点到目的地的旅程,然后返回到起点。我想得到任何可能的组合。
因此以下查询有效:
WITH outs AS(
SELECT from_, to_, departure, min(price)
FROM table_out
GROUP BY from_, to_, departure
),
ins AS (
SELECT from_, to_, departure, min(price)
FROM table_in
GROUP BY from_, to-, departure
)
SELECT DISTINCT on (from_, to_, departure, return)
a.from_, a.to_,
a.departure, b.departure as return,
a.price + b.price as totalprice
FROM outs a JOIN ins b ON (
a.from_ = b.to_
AND a.to_ = b.from_
AND a.departure <= b.departure
)
但是:如果我想从找到的最小组合中选择其他值,该怎么办?像a.name,b.name?我无法将这些参数添加到WITH
子选项中,因为我不想group by
这些属性。
无论如何,只要我拥有所有“最佳价格”组合,我就会对所选择的每个最佳价格项目的其他列感兴趣。
这可能吗?
答案 0 :(得分:1)
您可以使用row_number()
。在你的情况下:
WITH outs AS (
SELECT t.*
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY from_, to_, departure ORDER BY price DESC) as seqnum
FROM table_out
) t
WHERE seqnum = 1
),
ins AS (
SELECT t.*
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY from_, to_, departure ORDER BY price DESC) as seqnum
FROM table_in
) t
WHERE seqnum = 1
)
SELECT DISTINCT on (from_, to_, departure, return)
a.from_, a.to_,
a.departure, b.departure as return,
(a.price + b.price) as totalprice,
. . .
FROM outs a
JOIN ins b
ON a.from_ = b.to_ AND
a.to_ = b.from_ AND
a.departure <= b.departure
ORDRE BY from_, to_, departure, return;
或者,您也可以在子查询中使用DISTINCT ON
:
WITH outs AS (
SELECT DISTINCT ON (from_, to_, departure), t.*
FROM table_out
) t
WHERE seqnum = 1
ORDER BY from_, to_, departure, price
),
. . .
作为备注:这回答了你的问题。但是,您获得最便宜的往返旅行的方法并不能保证有效。考虑一下这三种联系的情况:
您的方法会选择(2)最便宜的出站连接。但是,当天为时已晚,没有随后的回归。如果要解决此问题,请使用示例数据和所需结果提出另一个问题。此答案解决了如何获取其他数据的问题。