Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XDCvS-0002H9-9r for pgsql-sql@arkaria.postgresql.org; Fri, 01 Aug 2014 13:31:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XDCvR-0001xu-OP for pgsql-sql@arkaria.postgresql.org; Fri, 01 Aug 2014 13:31:13 +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 1XDCvP-0001uu-Sh for pgsql-sql@postgresql.org; Fri, 01 Aug 2014 13:31:12 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XDCvG-0000iB-OW for pgsql-sql@postgresql.org; Fri, 01 Aug 2014 13:31:10 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by gateway1.nyi.internal (Postfix) with ESMTP id 3F45E22DA2 for ; Fri, 1 Aug 2014 09:30:59 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Fri, 01 Aug 2014 09:30:59 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=hBVwGfQBgvj6qe5/7XhycgYYBPk=; b=aNeGYWEm3SADD7bCcHag3VQkxAGZ dDIgCEbBQ9RGhgB3immBLW2fqgu73G3uHYIuLgp9mkJUfbHbkIpH6K/velrBZC2n 6+uh9VOJ9wskoIz9np9p+2dveT10/vUcGxAJj95cX/c72+310Al+JVE1hnVb0x00 L0GuobGj8PbZIpY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=hBVwGfQBgvj6qe5/7Xhycg YYBPk=; b=l1uYWv4hozr0GhxPpXkd0HBEPbCc5NH2wNhwI35qI9OaWSgDcO8thH zglzdjSJeomnXVMQfbcg2JjICjg/FHUONS537mTC5ALXuUXyv2Gr7HHf5vmUaTZ4 ZwPhnVefkLpWUv15Q5DfIPOCdv9DH39RXcXcE4EVJilw2UrqQ3G+k= X-Sasl-enc: WNQzsWKgYPlTf/l0KOKo4K9TSRmG9eqVn10yNH0DKb5h 1406899858 Received: from panda.site (unknown [174.21.176.242]) by mail.messagingengine.com (Postfix) with ESMTPA id C1020C007AD; Fri, 1 Aug 2014 09:30:58 -0400 (EDT) Message-ID: <53DB9691.8060409@aklaver.com> Date: Fri, 01 Aug 2014 06:30:57 -0700 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: DerekW , pgsql-sql@postgresql.org Subject: Re: PostgreSQL add id column that increments based on data References: <1406884541460-5813514.post@n5.nabble.com> In-Reply-To: <1406884541460-5813514.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.6 (-) 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 On 08/01/2014 02:15 AM, DerekW wrote: > I am relatively new to using Postgres, coming from a MySQL background. I am > using Postgres 9.3.4 on Windows x64. > > We are being supplied data in multiple fixed length text files. The first > digit on each line is a number between 1 and 4 that indicates the record > type of the data in that row. The rows are grouped sequentially such that > there will always first be a row of type 1 followed by zero or more rows of > the other types. > > data_x.txt > --------------------- > 1data01 > 2data02 > 4data03 > 4data04 > 1data05 > 1data06 > 3data07 > > To import this into Postgres I have used the following SQL commands: > > 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 indicator integer; > > UPDATE data_raw SET > indicator = CAST(substr(raw_data, 1, 1) AS integer), > raw_data = substr(raw_data, 2); > > I then create tables for each of the 4 record types: > > CREATE TABLE table_1 SELECT raw_data FROM data_raw WHERE indicator = 1; > CREATE TABLE table_2 SELECT raw_data FROM data_raw WHERE indicator = 2; > CREATE TABLE table_3 SELECT raw_data FROM data_raw WHERE indicator = 3; > CREATE TABLE table_4 SELECT raw_data FROM data_raw WHERE indicator = 4; > > What I need to do, but am unsure how, is to also add an "id" column for each > group where the indicator starts with 1. We will be getting weekly updates > so I need to specify the initial id for each batch. So if this batch starts > at id = 225, then I want to get the following tables from the sample data: > > table_1 > id | raw_data > -------------------- > 225 | data01 > 226 | data05 > 227 | data06 > > table_2 > id | raw_data > -------------------- > 225 | data02 > > table_3 > id | raw_data > -------------------- > 227 | data07 > > table_4 > id | raw_data > -------------------- > 225 | data03 > 225 | data04 I am not following the logic of the id numbering scheme. I see the sequential numbering for indicator 1. Not why indicator 2 uses 225 again when indicator 3 starts with 227 and indicator 4 has the same id for both rows. My guess, whatever the logic is, it is going to involve triggers and possibly another table to keep track of batch numbers > > > > > > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/PostgreSQL-add-id-column-that-increments-based-on-data-tp5813514.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql