agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: DerekW <derek@cmainfo.co.za>
To: pgsql-sql@postgresql.org
Subject: Re: PostgreSQL add id column that increments based on data
Date: Mon, 4 Aug 2014 01:35:18 -0700 (PDT)
Message-ID: <1407141318770-5813661.post@n5.nabble.com> (raw)
In-Reply-To: <1406909569687-5813543.post@n5.nabble.com>
References: <1406884541460-5813514.post@n5.nabble.com>
<1406909569687-5813543.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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-tp581...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
view thread (5+ messages)
Message-ID: <1407141318770-5813661.post@n5.nabble.com>
Permalink: ../1407141318770-5813661.post@n5.nabble.com/
Also on: postgresql.org/message-id/1407141318770-5813661.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: derek@cmainfo.co.za
Subject: Re: PostgreSQL add id column that increments based on data
In-Reply-To: <1407141318770-5813661.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox