Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何在Postgres中索引表中的子组?_Postgresql - Fatal编程技术网

Postgresql 如何在Postgres中索引表中的子组?

Postgresql 如何在Postgres中索引表中的子组?,postgresql,Postgresql,假设我有一张这样的桌子: select country_id, city_id, person_id from mytable country_id,city_id,person_id 123,45,100334 123,45,3460456 123,45,943875 123,121,4362 123,121,124747 146,87,3457320 146,89,3495879 146,89,34703924 我想对country_id和city_id的子组进行索引,以获得这样的结果

假设我有一张这样的桌子:

select country_id, city_id, person_id from mytable


country_id,city_id,person_id
123,45,100334
123,45,3460456
123,45,943875
123,121,4362
123,121,124747
146,87,3457320
146,89,3495879
146,89,34703924
我想对country_id和city_id的子组进行索引,以获得这样的结果:

select country_id, city_id, person_id, ???, ??? from mytable

country_id,city_id,person_id,country_num,city_num
123,45,100334,1,1
123,45,3460456,1,1
123,45,943875,1,1
123,121,4362,1,2
123,121,124747,1,2
146,87,3457320,2,1
146,89,3495879,2,2
146,89,34703924,2,2
换句话说,我想用从1开始的整数来计算序列中的所有国家,并且我想在每个国家内分别以相同的方式标记城市。在博士后中有没有一种优雅的方法可以做到这一点?

使用:

SELECT
    *,
    dense_rank() OVER (ORDER BY country_id),
    dense_rank() OVER (PARTITION BY country_id ORDER BY city_id)
FROM
    mytable