简体   繁体   English

PostgreSQL:如何通过将所选行的列设置为递增整数来更改表?

[英]PostgreSQL: how can I Alter table by setting a column of the selected rows to be increasing integers?

I have this table named issue with thousands of rows:我有这个名为issue的表,有数千行:

id ID status地位 listPosition列表位置
43 43 New新的 0 0
44 44 New新的 0 0
45 45 New新的 0 0
46 46 Accepted公认 0 0
51 51 Accepted公认 1 1
54 54 Accepted公认 0 0
66 66 processing加工 0 0
68 68 processing加工 0 0

I want to select all rows whose status is a specific value, like New , and update the listPosition value such that they are in increasing indexes, like 1, 2, 3, ....我想 select 所有status为特定值的行,如New ,并更新listPosition值,使它们处于递增索引中,如 1、2、3、...。

the result should be like:结果应该是这样的:

id ID status地位 listPosition列表位置
43 43 New新的 1 1
44 44 New新的 2 2
45 45 New新的 3 3
46 46 Accepted公认 1 1
51 51 Accepted公认 2 2
54 54 Accepted公认 3 3
66 66 processing加工 1 1
68 68 processing加工 2 2

How can I do that in PostgreSql?我怎么能在 PostgreSql 中做到这一点?

I tried:我试过了:

UPDATE issue SET listPosition= 'dummy.listPosition'
FROM  
(
  SELECT id, row_number() over (partition by status order by id) AS "listPosition" FROM issue
) AS dummy
WHERE issue.id= 'dummy.id';

and it gives this error:它给出了这个错误:

ERROR:  invalid input syntax for type integer: "dummy.id"
LINE 6: WHERE issue.id= 'dummy.id';

How did it fail?它是如何失败的?

I tried both: (don't worry about the dstOrgId thing)我都试过了:(不用担心dstOrgId的事情)


UPDATE issue SET listPosition= "dummy.listPosition"
FROM  
(
  SELECT id, row_number() over (partition by "dstOrgId", status order by id) AS "listPosition" FROM issue order by id
) AS dummy
WHERE issue.id = dummy.id;



WITH dummy AS
(
  SELECT id, row_number() over (partition by "dstOrgId", status order by id) AS "listPosition" FROM issue order by id
) 
UPDATE issue set listPosition = "dummy.listPosition"
FROM dummy
WHERE issue.id = dummy.id; 

They all gives error:他们都给出了错误:

ERROR:  column "dummy.listPosition" does not exist
LINE 5: UPDATE issue set listPosition = "dummy.listPosition"

Why on earth does it not exist???为什么它在地球上不存在??? PostgreSQL syntax is so freaking strange, since it asks me to change set listPosition = dummy.listPosition to set listPosition = "dummy.listPosition" , and complains that column does not exist. PostgreSQL 语法太奇怪了,因为它要求我将set listPosition = dummy.listPosition更改为set listPosition = "dummy.listPosition" ,并抱怨该列不存在。

what????什么????


I have finally got the working command through trial and error:我终于通过反复试验得到了工作命令:

UPDATE issue SET "listPosition"= dummy."listPosition"
FROM  
(
  SELECT id, row_number() over (partition by "dstOrgId", status order by id) AS "listPosition" FROM issue order by id
) AS dummy
WHERE issue.id = dummy.id;

what is the significance of double quote and the difference between single quote and double quote in PostgreSQL anyways?无论如何,双引号的意义以及PostgreSQL中的单引号和双引号之间的区别是什么?

You can achieve it by using row_number .您可以通过使用row_number来实现。

select   id
        ,status
        ,row_number() over (partition by status order by id) as listPosition
from     t 
order by id, status
id ID status地位 listposition列表位置
43 43 New新的 1 1
44 44 New新的 2 2
45 45 New新的 3 3
46 46 Accepted公认 1 1
51 51 Accepted公认 2 2
54 54 Accepted公认 3 3
66 66 processing加工 1 1
68 68 processing加工 2 2

Fiddle 小提琴

I have finally got the working command through trial and error:我终于通过反复试验得到了工作命令:

UPDATE issue SET "listPosition"= dummy."listPosition"
FROM  
(
  SELECT id, row_number() over (partition by "dstOrgId", status order by id) AS "listPosition" FROM issue order by id
) AS dummy
WHERE issue.id = dummy.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM