[英]Postgres window functions - rank() partition by bigint
I'm trying to rank data over partition by a column thats type is bigint but it isn't working when I change the column in the partition by to a column of type timestamp with time zone it does work. 我正在尝试按类型为bigint的列对分区上的数据进行排序,但是当我将分区中的列更改为具有时区的timestamp类型的列时,它不起作用。
My query looks like this: 我的查询如下所示:
select id,ts_insert,serial_no,rank() over (partition by serial_no order by serial_no desc) from schema.table_name
The result looks like this: 结果看起来像这样:
1,'2019-02-19 18:27:50.258846+11',19516,1
1,'2019-02-19 18:27:50.258846+11',19515,1
1,'2019-02-19 18:27:50.258846+11',19514,1
1,'2019-02-06 19:45:38.896062+11',15069,1
1,'2019-02-05 21:46:28.633325+11',14827,1
Expected result is: 预期结果是:
1,'2019-02-19 18:27:50.258846+11',19516,1
1,'2019-02-19 18:27:50.258846+11',19515,2
1,'2019-02-19 18:27:50.258846+11',19514,3
1,'2019-02-06 19:45:38.896062+11',15069,4
1,'2019-02-05 21:46:28.633325+11',14827,5
Can someone help me please? 有人能帮助我吗?
Thanks in advance... 提前致谢...
You seem to want: 您似乎想要:
select id, ts_insert, serial_no,
rank() over (partition by id order by serial_no desc)
from schema.table_name;
serial_no
is unique (at least in the data you've provided). serial_no
是唯一的(至少在您提供的数据中)。 If you partition by a unique column, then the rank()
will always be 1
. 如果按唯一列进行分区,则
rank()
始终为1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.