PostgreSQL add id column that increments based on data -


I am relatively new to using postgrades coming from a MySQL background. I am using PostGrace 9.3.4 on Windows X64.

Data is being supplied to us in several definite length text files The first digit on each line is a number between 1 and 4, which indicates the record type of the data in that row, respectively Are grouped in form, because there will always be a first row and after this there will be other types of zeros or more rows.

  data_x.txt --------- ------------ 1data01 2data02 4data03 4data04 1data05 1data06 3data07  
< P> I used the following SQL commands to import it into Postgrass:

  Create table datadown (raw_data TEXT); Cpp data_word from 'c: \ path \ data_x.txt' ... ...; - Repeated optional table data_raw column indicator indicator for each file; UPDATE data_raw SET indicator = CAST (substr (raw_data, 1, 1) AS integer), raw_data = substr (raw_data, 2);  

I create tables for each of the 4 record types:

  Create table table 1 Select raw_data from data_off WHERE indicator = 1; Select table table 2 raw_data FROM data_raw where pointer = 2; Create table table - select raw_data related to data WHERE indicator = 3; Create table table - select raw_data from data_by WHERE indicator = 4;  

What I have to do, but uncertain how to add an "id" column to each group, where the indicator starts from 1. We will be getting weekly updates so that 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 table from sample data:

< Pre> table_1 id | Raw_data -------------------- 225 | Data 016 226 | Data05 227 | Data06 table_2 ID Raw_data -------------------- 225 | Data02 table_3 ID | Raw_data -------------------- 227 | Data07 table_4 ID | Raw_data -------------------- 225 | Data 03 225 | Data 04

Try something like this to generate an ID for each data set: < / P> Data_w

as

  SELECT sum (when indicator = 1 then 1 and 0 end) over (order / some To define the order * /) will generate a  id_base  for each data group if you need to start with a particular ID - just add this ID to  id_base  . 


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -