Re: PostgreSQL add id column that increments based on data - Mailing list pgsql-sql

From DerekW
Subject Re: PostgreSQL add id column that increments based on data
Date
Msg-id 1407141318770-5813661.post@n5.nabble.com
Whole thread Raw
In response to Re: PostgreSQL add id column that increments based on data  (David G Johnston <david.g.johnston@gmail.com>)
List pgsql-sql
Thank you for the input.

I have come up with the following solution:

CREATE TABLE data_raw (   raw_data TEXT
);

COPY data_raw FROM 'C:\path\data_x.txt' ...; -- Repeated for each file

ALTER TABLE data_raw   ADD COLUMN pk_id serial,   ADD COLUMN id integer,   ADD COLUMN indicator integer;

UPDATE data_raw SET   indicator = CAST(substr(raw_data, 1, 1) AS integer),   raw_data = substr(raw_data, 2);

CREATE TABLE id_base AS
SELECT   pk_id,   sum(CASE WHEN indicator = 1 THEN 1 ELSE 0 END) OVER (ORDER BY pk_id) AS
rec_id
FROM data_raw;

CREATE INDEX id_base_pk ON id_base USING btree(pk_id);

UPDATE data_raw r SET   id = (SELECT rec_id FROM id_base b WHERE b.pk_id = r.pk_id);

DROP TABLE id_base;





--
View this message in context:
http://postgresql.1045698.n5.nabble.com/PostgreSQL-add-id-column-that-increments-based-on-data-tp5813514p5813661.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



pgsql-sql by date:

Previous
From: David G Johnston
Date:
Subject: Re: PostgreSQL add id column that increments based on data
Next
From: Marcin Krawczyk
Date:
Subject: function call