我有两个这样的表:
Nu
i j
1 2
1 3
1 4
Nv
i j
2 1
2 5
3 1
3 6
我要做的是找到:
从Nu UNION中选择j对于每个不同的区域(Nu.i和Nv.i),从Nv选择j)
喜欢这个:
Nu.i Nv.i v
1 2 2
1 2 3
1 2 4
1 2 1
1 2 5
1 3 2
1 3 3
1 3 4
1 3 1
1 3 6
我可以通过Vertica SQL查询吗?
我尝试过:
Select
Nu.i,
Nv.i,
(Select j from Nu UNION Select j from Nv group by Nv.j) as v
from Nu, Nv;
错误:
ERROR 4840: Subquery used as an expression returned more than one row
然后:
Select
Nu.i,
Nv.i,
(Select j from Nu UNION Select j from Nv) as v
from Nu, Nv
group by Nu.i, Nv.i;
错误:
如果子查询不支持SELECT或ORDER BY中的子查询 不属于GROUP BY
请让我知道你的建议。
答案 0 :(得分:0)
您的结果表在某种程度上对我来说是意外的-我无法弄清楚如何生成它的规则。
Nu有3行。 Nv有4行。我当时在想两个表之间的CROSS JOIN,但这将导致3 x 4,即12行。像这样:
WITH
Nu (i,j) AS (
SELECT 1,2
UNION ALL SELECT 1,3
UNION ALL SELECT 1,4
)
,
Nv(i,j) AS (
SELECT 2,1
UNION ALL SELECT 2,5
UNION ALL SELECT 3,1
UNION ALL SELECT 3,6
)
SELECT
Nu.i AS "Nu.i"
, Nv.i AS "Nv.i"
, Nu.j AS "Nu.j"
, Nv.j AS "Nv.j"
FROM Nu CROSS JOIN Nv;
-- out Nu.i | Nv.i | Nu.j | Nv.j
-- out ------+------+------+------
-- out 1 | 2 | 2 | 1
-- out 1 | 2 | 3 | 1
-- out 1 | 2 | 4 | 1
-- out 1 | 2 | 2 | 5
-- out 1 | 2 | 3 | 5
-- out 1 | 2 | 4 | 5
-- out 1 | 3 | 2 | 1
-- out 1 | 3 | 3 | 1
-- out 1 | 3 | 4 | 1
-- out 1 | 3 | 2 | 6
-- out 1 | 3 | 3 | 6
-- out 1 | 3 | 4 | 6
-- out (12 rows)
-- out
-- out Time: First fetch (12 rows): 14.037 ms. All rows formatted: 14.086 ms
但是可能是,看到此结果表,您可以自己找出其余的吗?