我需要在表中的列中找到所有唯一可能的值組合。例如,對於列值1,2,3,4,5。我想要的結果是[1,2],[1,3],[1,4],[1,5],[2,1],[2,3]等組合的唯一值列中的一列
指針構造查詢以查找值的組合。
感謝
我需要在表中的列中找到所有唯一可能的值組合。例如,對於列值1,2,3,4,5。我想要的結果是[1,2],[1,3],[1,4],[1,5],[2,1],[2,3]等組合的唯一值列中的一列
指針構造查詢以查找值的組合。
感謝
一個cross join
可能是有用的。
看到這個演示:http://www.sqlfiddle.com/#!12/59af5/1
的ANSI SQL語法使用CROSS JOIN
操作:
create table val(x int);
insert into val values(1),(2),(3),(4),(5);
SELECT a.x a, b.x b
FROM val a
CROSS JOIN val b
WHERE a.x <> b.x
ORDER BY a,b;
沒有CROSS JOIN
應該大多數DBMS系統上運行該查詢的另一種形式,但建議ANSI形式清晰度:
SELECT a.x a, b.x b
FROM val a, val b
WHERE a.x <> b.x
ORDER BY a,b;
請注意,大數據集的交叉連接可能會導致數據庫性能下降,對於100個值,它會生成100x100 = 10.000行,對於1000 - > 1.000.000行。
您可以通過使用添加常量鍵值的子查詢來在BigQuery中進行交叉連接,然後加入該常量值。
例如,這裏是一個查詢,將計算的交叉聯接{1,2,3}和{2,4,6}:
SELECT t1.num as first, t2.num as second
FROM (
SELECT num, 1 as key
FROM (
SELECT 1 as num), (
SELECT 2 as num), (
SELECT 3 as num)) as t1
JOIN (
SELECT num, 1 as key
FROM (
SELECT 2 as num), (
SELECT 4 as num), (
SELECT 6 as num)) as t2
ON t1.key = t2.key
WHERE t1.num <> t2.num
注意此使用大量查詢 「特技」,以創建兩個輸入表。如果你只是用現有的表做這件事,它看起來像:
SELECT t1.num as first, t2.num as second
FROM (
SELECT foo as num, 1 as key
FROM [my_dataset.my_table]) as t1
JOIN (
SELECT foo as num, 1 as key
FROM [my_dataset.my_table]) as t2
ON t1.key = t2.key
WHERE t1.num <> t2.num
感謝您的迴應kordirko。我正在尋找一種方法在google bigquery中執行此操作。 – sunil