唯一值的组合表中的列

时间:2013-09-28 05:05:50

标签: sql google-bigquery

我需要在表格的列中找到所有唯一可能的值组合。例如,对于列值1,2,3,4,5。我希望结果是[1,2],[1,3],[1,4],[1,5],[2,1],[2,3]等。

将会欣赏构造查询以查找值组合的任何指针。

谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用添加常量键值的子选择,然后连接该常量值,在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

请注意,这使用BigQuery“技巧”来创建两个输入表。如果您只是使用现有表执行此操作,它将如下所示:

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

答案 1 :(得分:1)

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行。