说我有一些数据:
grp v1 v2
--- -- --
2 5 7
2 4 9
3 10 2
3 11 1
我想创建与表的排序无关的新列 - 这样两列具有独立的排序,即按v1排序,与v2无关,而按grp分区。
结果(独立排序,由grp分区)将是:
grp v1 v2 v1_ordered v2_ordered
--- -- -- ---------- ----------
2 5 7 4 7
2 4 9 5 9
3 10 2 10 1
3 11 1 11 2
执行此操作的一种方法是创建两个表和CROSS JOIN。但是,我使用太多行数据才能使计算更容易处理 - 有没有办法在没有JOIN的单个查询中执行此操作?
基本上,我喜欢写SQL:
SELECT
*,
v1 OVER (PARTITION BY grp ORDER BY v1 ASC) as v1_ordered,
v2 OVER (PARTITION BY grp ORDER BY v2 ASC) as v2_ordered
FROM [example_table]
这会破坏表格行,但它是许多应用程序的必要功能 - 例如计算两个字段之间的有序关联CORR(v1_ordered, v2_ordered).
这可能吗?
答案 0 :(得分:1)
我认为你的方向正确!你只需要使用适当的窗口功能。在这种情况下Row_number()。它应该工作!
根据@cgn请求添加工作示例:
我不认为有办法完全避免使用JOIN
同时下面的示例在其他答案中仅使用 ONE JOIN vs TWO JOIN :
SELECT
a.grp AS grp,
a.v1 AS v1,
a.v2 AS v2,
a.v1 AS v1_ordered,
b.v2 AS v2_ordered
FROM (
SELECT grp, v1, v2, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY v1) AS v1_order
FROM [example_table]
) AS a
JOIN (
SELECT grp, v1, v2, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY v2) AS v2_order
FROM [example_table]
) AS b
ON a.grp = b.grp AND a.v1_order = b.v2_order
结果符合预期:
grp v1 v2 v1_ordered v2_ordered
2 4 9 4 7
2 5 7 5 9
3 10 2 10 1
3 11 1 11 2
现在你可以使用如下的CORR()
SELECT grp, CORR(v1_ordered, v2_ordered) AS [corr]
FROM (
SELECT
a.grp AS grp,
a.v1 AS v1,
a.v2 AS v2,
a.v1 AS v1_ordered,
b.v2 AS v2_ordered
FROM (
SELECT grp, v1, v2, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY v1) AS v1_order
FROM [example_table]
) AS a
JOIN (
SELECT grp, v1, v2, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY v2) AS v2_order
FROM [example_table]
) AS b
ON a.grp = b.grp AND a.v1_order = b.v2_order
)
GROUP BY grp
答案 1 :(得分:1)
这对你有用。
的 SQLFiddle Demo in SQL Server
强>
注意:示例中提到的序列不一定是从数据库返回行的方式。在我的情况下,对于v1
,我得4,5,10,11
与5,4,10,11
不同。但是,您的输出将与您想要的相同。
Select t.grp,t.v1,t.v2,
v1.v1 as v1_ordered,v2.v2 as v2_ordered
From
(
select t1.*,
row_number() over (partition by grp
Order by v1) v1o
,
row_number() over (partition by grp
Order by v2) v2o
from table1 t1
) t
Inner join
(
Select t.*,
row_number() over (partition by grp
Order by v1) v1o
From table1 t
) v1
On t.grp=v1.grp
And t.v1o=v1.v1o
Inner join
(
Select t.*,
row_number() over (partition by grp
Order by v2) v2o
From table1 t
) v2
On t.grp=v2.grp
And t.v1o=v2.v2o
输出:
+------+-----+-----+-------------+------------+
| grp | v1 | v2 | v1_ordered | v2_ordered |
+------+-----+-----+-------------+------------+
| 2 | 4 | 9 | 4 | 7 |
| 2 | 5 | 7 | 5 | 9 |
| 3 | 10 | 2 | 10 | 1 |
| 3 | 11 | 1 | 11 | 2 |
+------+-----+-----+-------------+------------+
答案 2 :(得分:0)
我不是百分百肯定这可以在BigQuery中运行,但现在就是:
select e.*, ev1.v1, ev2.v2
from (select e.*,
row_number() over (partition by grp order by v1) as seqnum_v1,
row_number() over (partition by grp order by v2) as seqnum_v2
from example e
) e join
(select e.*, row_number() over (partition by grp order by v1) as seqnum_v1
from example e
) ev1
on ev1.grp = e.grp and ev1.seqnum_v1 = e.seqnum_v1 join
(select e.*, row_number() over (partition by grp order by v2) as seqnum_v2
from example e
) ev2
on ev2.grp = e.grp and ev2.seqnum_v2 = e.seqnum_v2;
这个想法是为每个列分配一个独立的排序。然后连接回原始表以获取实际值。