Domanda

Problem: to set row number in columns' value in Vertica.

For example:

Table T has two columns: Id, name

I want to use a script to add the row number in the value of the name. In mySQl, I run the following script to update:

set @i=0;
update T set name = (CONCAT(name, (@i:=@i+1)));

However, Vertica doesn't support variables.

Could you please provide a way to reach the target?

È stato utile?

Soluzione

As Vertica supports window functions, something like this can be used to retrieve this data:

select name, 
       row_number() over (order by name) as rn
from T;

I am not sure how this could be moved into an UPDATE statement though - I don't have a vertica installation available:

update T
  set name = name || tx.rn
from (
   select id, 
          row_number() over (order by name) as rn
   from T
) as tx
where tx.id = t.id;

I don't know if that qualifies as a "self-join" which isn't allowed. But maybe that points you into the right direction.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top