PSQL:ROW_NUMBER 连续递增
PSQL: ROW_NUMBER incremented continuously
您好,有以下表格 T1:
field1 | field3
--------+--------
A1 | foo
A2 | v1
A3 | v2
A4 | bar
和 T2:
field2 | field3
--------+--------
B1 | foo
B2 | bar
如果我执行以下请求:
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
ROW_NUMBER () OVER (ORDER BY T2.field3) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3
...我得到以下结果:
F2:B1, F3:foo, F4:1
F2:B2, F3:bar, F4:4 // I would like F4:2
但我希望 F4 一个一个递增...我认为这是因为与 T1 的连接,但我不知道如何隔离 ROW_NUMBER...
有效
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
ROW_NUMBER () OVER (ORDER BY T2.field3 DESC) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3;
也有效
和
DENSE_RANK() 不会重复计算
在第 302 行,我回顾了为什么我使用 DENSE_RANK 而不是 ROW_NUMBER 或 RANK https://github.com/pavankat/fantasy-football/blob/master/db/queries.sql
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
DENSE_RANK() OVER (ORDER BY T2.field3 DESC) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3;
您好,有以下表格 T1:
field1 | field3
--------+--------
A1 | foo
A2 | v1
A3 | v2
A4 | bar
和 T2:
field2 | field3
--------+--------
B1 | foo
B2 | bar
如果我执行以下请求:
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
ROW_NUMBER () OVER (ORDER BY T2.field3) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3
...我得到以下结果:
F2:B1, F3:foo, F4:1
F2:B2, F3:bar, F4:4 // I would like F4:2
但我希望 F4 一个一个递增...我认为这是因为与 T1 的连接,但我不知道如何隔离 ROW_NUMBER...
有效
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
ROW_NUMBER () OVER (ORDER BY T2.field3 DESC) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3;
也有效 和 DENSE_RANK() 不会重复计算
在第 302 行,我回顾了为什么我使用 DENSE_RANK 而不是 ROW_NUMBER 或 RANK https://github.com/pavankat/fantasy-football/blob/master/db/queries.sql
SELECT DISTINCT ON (T2.field2, T2.field3)
T2.field2 AS F2,
T2.field3 AS F3,
DENSE_RANK() OVER (ORDER BY T2.field3 DESC) AS F4
FROM T2
JOIN T1 ON T2.field3=T1.field3;