Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XEDju-0008Pr-UX for pgsql-sql@arkaria.postgresql.org; Mon, 04 Aug 2014 08:35:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XEDjt-00014v-UE for pgsql-sql@arkaria.postgresql.org; Mon, 04 Aug 2014 08:35:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XEDjt-00014N-7k for pgsql-sql@postgresql.org; Mon, 04 Aug 2014 08:35:29 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XEDjn-00072s-5e for pgsql-sql@postgresql.org; Mon, 04 Aug 2014 08:35:27 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1XEDji-00063d-P2 for pgsql-sql@postgresql.org; Mon, 04 Aug 2014 01:35:18 -0700 Date: Mon, 4 Aug 2014 01:35:18 -0700 (PDT) From: DerekW To: pgsql-sql@postgresql.org Message-ID: <1407141318770-5813661.post@n5.nabble.com> In-Reply-To: <1406909569687-5813543.post@n5.nabble.com> References: <1406884541460-5813514.post@n5.nabble.com> <1406909569687-5813543.post@n5.nabble.com> Subject: Re: PostgreSQL add id column that increments based on data MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org 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. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql