鉴于当前 Postgres 9.4 中的此设置(来自此相关问题):
CREATE TABLE foo (ts, foo) AS
VALUES (1, 'A') -- int, text
, (7, 'B');
CREATE TABLE bar (ts, bar) AS
VALUES (3, 'C')
, (5, 'D')
, (9, 'E');
Run Code Online (Sandbox Code Playgroud)
db<> fiddle here(也来自上一个问题)。
我SELECT用 a写了一个FULL JOIN来实现引用问题的目标。简化:
SELECT ts, f.foo, b.bar
FROM foo f
FULL JOIN bar b USING (ts);Run Code Online (Sandbox Code Playgroud)
As per specifications, the correct way to address the column ts is without table qualification. Either of the …
这个问题是我之前提出的一个过于简化的问题的扩展。更准确的示例在此 SQLFiddle中演示,我演示了一个有效(但速度较慢)的解决方案,然后尝试将先前的答案调整为实际问题。
实际问题是因为这两个表包含多个时间线的事件。
CREATE TABLE foo (ts int, id text, foo text);
INSERT INTO foo (ts, id, foo)
VALUES
(1, 'A', 'Lorem'),
(1, 'B', 'ipsum'),
(4, 'B', 'dolor'),
(5, 'A', 'sit'),
(8, 'A', 'amet'),
(8, 'B', 'consectetur');
CREATE TABLE bar (ts int, id text, bar text);
INSERT INTO bar (ts, id, bar)
VALUES
(1, 'A', 'adipiscing'),
(5, 'B', 'elit'),
(6, 'A', 'sed'),
(9, 'B', 'do ');
Run Code Online (Sandbox Code Playgroud)
每个表都有时间线“A”和“B”的事件。目标是将结果组合成单个结果集,显示每个时间线的“状态”。两条时间线是正交的。
ts id foo 栏 1 Lorem adipiscing …