pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Add columns to table; insert values based on row
12+ messages / 5 participants
[nested] [flat]

* Add columns to table; insert values based on row
@ 2018-11-01 20:23 Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:32 ` Re: Add columns to table; insert values based on row Adrian Klaver <adrian.klaver@aklaver.com>
  2018-11-01 20:34 ` Re: Add columns to table; insert values based on row David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 12+ messages in thread

From: Rich Shepard @ 2018-11-01 20:23 UTC (permalink / raw)
  To: pgsql-general

   I have the following code in a script:

alter table stations add column start_date date;
alter table stations add column end_date date;
alter table stations add column howmany integer;
alter table stations add column bin_col char(8);

insert into stations (start_date, end_date, howmany, bin_col) values ( )
   select site_nbr from stations
     where site_nbr = ' ';

   The table has 82 rows. Is there a more elegant way to insert data specific
to a site_nbr other than 82 repetitions of the insert statement? (I suspect
not, but I might be wrong and learn something valuable by asking.)

Regards,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-01 20:32 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-11-01 20:50   ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 12+ messages in thread

From: Adrian Klaver @ 2018-11-01 20:32 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general

On 11/1/18 1:23 PM, Rich Shepard wrote:
>    I have the following code in a script:
> 
> alter table stations add column start_date date;
> alter table stations add column end_date date;
> alter table stations add column howmany integer;
> alter table stations add column bin_col char(8);
> 
> insert into stations (start_date, end_date, howmany, bin_col) values ( )
>    select site_nbr from stations
>      where site_nbr = ' ';

I am not following the above.

Are you trying to add new data to existing records?

If so where is the new data coming from?

> 
>    The table has 82 rows. Is there a more elegant way to insert data 
> specific
> to a site_nbr other than 82 repetitions of the insert statement? (I suspect
> not, but I might be wrong and learn something valuable by asking.)
> 
> Regards,
> 
> Rich
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:32 ` Re: Add columns to table; insert values based on row Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-11-01 20:50   ` Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 21:28     ` Re: Add columns to table; insert values based on row Tim Cross <theophilusx@gmail.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Rich Shepard @ 2018-11-01 20:50 UTC (permalink / raw)
  To: pgsql-general

On Thu, 1 Nov 2018, Adrian Klaver wrote:

>> alter table stations add column start_date date;
>> alter table stations add column end_date date;
>> alter table stations add column howmany integer;
>> alter table stations add column bin_col char(8);
>> 
>> insert into stations (start_date, end_date, howmany, bin_col) values ( )
>>    select site_nbr from stations
>>      where site_nbr = ' ';

> Are you trying to add new data to existing records?

Adrian,

   I am adding four columns to an existing table that already contains four
columns.

> If so where is the new data coming from?

   I have a text file and will fill each insert statement by hand if there's
not a more efficient way to do this.

Regards,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:32 ` Re: Add columns to table; insert values based on row Adrian Klaver <adrian.klaver@aklaver.com>
  2018-11-01 20:50   ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-01 21:28     ` Tim Cross <theophilusx@gmail.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Tim Cross @ 2018-11-01 21:28 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general


Rich Shepard <rshepard@appl-ecosys.com> writes:

> On Thu, 1 Nov 2018, Adrian Klaver wrote:
>
>>> alter table stations add column start_date date;
>>> alter table stations add column end_date date;
>>> alter table stations add column howmany integer;
>>> alter table stations add column bin_col char(8);
>>> 
>>> insert into stations (start_date, end_date, howmany, bin_col) values ( )
>>>   select site_nbr from stations
>>>   where site_nbr = ' ';
>
>> Are you trying to add new data to existing records?
>
> Adrian,
>
>    I am adding four columns to an existing table that already contains four
> columns.
>
>> If so where is the new data coming from?
>
>    I have a text file and will fill each insert statement by hand if there's
> not a more efficient way to do this.
>
> Regards,
>
> Rich

Like others, I'm not clear on exactly what your after here, but did want
to point out

1. If your doing it by hand, you don't have to do a separate 'full'
insert statement for every row i.e.

insert into blah (x, y, z) values
(....),
(....),
(....),
....
(...);

is valid syntax. You don't need to do a full "insert into blah (....)
values (...)" for each insert.

2. If it really is an insert you want to do and you already have the
data in a file e.g. CSV or similar, then you can use the \copy command
to process the file, which is very fast.

3. Is it really insert or update you need?


-- 
Tim Cross




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-01 20:34 ` David G. Johnston <david.g.johnston@gmail.com>
  2018-11-01 20:53   ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 12+ messages in thread

From: David G. Johnston @ 2018-11-01 20:34 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general

On Thu, Nov 1, 2018 at 1:26 PM Rich Shepard <rshepard@appl-ecosys.com>
wrote:

>    I have the following code in a script:
>
> alter table stations add column start_date date;
> alter table stations add column end_date date;
> alter table stations add column howmany integer;
> alter table stations add column bin_col char(8);
>
> insert into stations (start_date, end_date, howmany, bin_col) values ( )
>    select site_nbr from stations
>      where site_nbr = ' ';
>
>    The table has 82 rows. Is there a more elegant way to insert data
> specific
> to a site_nbr other than 82 repetitions of the insert statement? (I suspect
> not, but I might be wrong and learn something valuable by asking.)
>
>
That makes no sense to me...you already have 82 rows on the table so if you
insert 82 more you'll have 164 which doesn't seem like what you would
want...

I would probably do:

CREATE TABLE stations_ext (site_nbr, start_date date, ...)

COPY stations_ext FROM filename;

UPDATE stations SET start_date = stations_ext.start_date, ...
FROM stations_ext
WHERE stations.site_nbr = stations_ext.site_nbr;

OR

UPDATE stations SET start_date = 'literal date'::date WHERE site_nbr =
'literal site number';
... 82 more times as appropriate
But I would build out those UPDATE statements in a spreadsheet
Either way I'd get the relevant new data into tabular format with a
site_nbr associated first.

David J.

^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:34 ` Re: Add columns to table; insert values based on row David G. Johnston <david.g.johnston@gmail.com>
@ 2018-11-01 20:53   ` Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:57     ` Re: Add columns to table; insert values based on row Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Rich Shepard @ 2018-11-01 20:53 UTC (permalink / raw)
  To: pgsql-general

On Thu, 1 Nov 2018, David G. Johnston wrote:

> That makes no sense to me...you already have 82 rows on the table so if you
> insert 82 more you'll have 164 which doesn't seem like what you would
> want...
>
> I would probably do:

David,

   The table already exists with four attribute columns and their data. I'm
adding four new columns and their data base on the existing row's value in
column site_nbr.

   Based on your and Adrian's responses I'll go ahead and enter the values
filled intert statements for each row.

Thanks,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:34 ` Re: Add columns to table; insert values based on row David G. Johnston <david.g.johnston@gmail.com>
  2018-11-01 20:53   ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-01 20:57     ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-11-01 20:58       ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Adrian Klaver @ 2018-11-01 20:57 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general

On 11/1/18 1:53 PM, Rich Shepard wrote:
> On Thu, 1 Nov 2018, David G. Johnston wrote:
> 
>> That makes no sense to me...you already have 82 rows on the table so 
>> if you
>> insert 82 more you'll have 164 which doesn't seem like what you would
>> want...
>>
>> I would probably do:
> 
> David,
> 
>    The table already exists with four attribute columns and their data. I'm
> adding four new columns and their data base on the existing row's value in
> column site_nbr.
> 
>    Based on your and Adrian's responses I'll go ahead and enter the values
> filled intert statements for each row.

Why? As David said that will get you an additional 82 rows with data 
that is dissociated from the existing data. You want to update the 
existing rows with the new field data for each station.

> 
> Thanks,
> 
> Rich
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:34 ` Re: Add columns to table; insert values based on row David G. Johnston <david.g.johnston@gmail.com>
  2018-11-01 20:53   ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-01 20:57     ` Re: Add columns to table; insert values based on row Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-11-01 20:58       ` Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Rich Shepard @ 2018-11-01 20:58 UTC (permalink / raw)
  To: pgsql-general

On Thu, 1 Nov 2018, Adrian Klaver wrote:

> Why? As David said that will get you an additional 82 rows with data that is 
> dissociated from the existing data. You want to update the existing rows with 
> the new field data for each station.

   D'oh! Of course!

Thanks both,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
@ 2018-11-01 21:56 Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-02 00:08 ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Rich Shepard @ 2018-11-01 21:56 UTC (permalink / raw)
  To: pgsql-general

On Fri, 2 Nov 2018, Tim Cross wrote:

> 3. Is it really insert or update you need?

Tim,

   Update.

   It's been a long time since I did any database manipulation (I just use
the existing databases as each project needs its data) so I sometimes make
these types of mistakes.

   Your idea of manipulating a text file is a great one!

   I'll use gawk to extract the relevant fields from the text file in which
they reside (in the correct order), then use emacs keyboard macros to add
the appropriate update text to each line. Must more efficient (and less
tedious) than manually writing each line.

Thanks,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 21:56 Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-02 00:08 ` Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-02 01:34   ` Re: Add columns to table; insert values based on row Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Rich Shepard @ 2018-11-02 00:08 UTC (permalink / raw)
  To: pgsql-general

On Thu, 1 Nov 2018, Rich Shepard wrote:

> I'll use gawk to extract the relevant fields from the text file in which
> they reside (in the correct order), then use emacs keyboard macros to add
> the appropriate update text to each line. Must more efficient (and less
> tedious) than manually writing each line.

   Actually a one-liner gawk script does the job.

   Thanks again for the suggestion!

Regards,

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 21:56 Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-02 00:08 ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
@ 2018-11-02 01:34   ` Ken Tanzer <ken.tanzer@gmail.com>
  2018-11-02 12:28     ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Ken Tanzer @ 2018-11-02 01:34 UTC (permalink / raw)
  To: rshepard@appl-ecosys.com; +Cc: pgsql-general

On Thu, Nov 1, 2018 at 5:08 PM Rich Shepard <rshepard@appl-ecosys.com>
wrote:

> On Thu, 1 Nov 2018, Rich Shepard wrote:
>
> > I'll use gawk to extract the relevant fields from the text file in which
> > they reside (in the correct order), then use emacs keyboard macros to add
> > the appropriate update text to each line. Must more efficient (and less
> > tedious) than manually writing each line.
>
>    Actually a one-liner gawk script does the job.
>

I'm not sure what format your text file is in, but you could probably use
\copy to bring it into a (temporary) table in Postgres.  As long as it had
your four new columns and the site_nbr, you could then update from that
table in one swoop:

UPDATE stations
SET start_date=new_data.start_date,
end_date=...
FROM new_data
WHERE stations.site_nbr=new_data.site_nbr;

Cheers,
Ken


-- 
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken.tanzer@agency-software.org
(253) 245-3801

Subscribe to the mailing list
<agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.

^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Add columns to table; insert values based on row
  2018-11-01 21:56 Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-02 00:08 ` Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
  2018-11-02 01:34   ` Re: Add columns to table; insert values based on row Ken Tanzer <ken.tanzer@gmail.com>
@ 2018-11-02 12:28     ` Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Rich Shepard @ 2018-11-02 12:28 UTC (permalink / raw)
  To: pgsql-general

On Thu, 1 Nov 2018, Ken Tanzer wrote:

> I'm not sure what format your text file is in, but you could probably use
> \copy to bring it into a (temporary) table in Postgres. As long as it had
> your four new columns and the site_nbr, you could then update from that
> table in one swoop:

Ken,

   Thank you.

Rich




^ permalink  raw  reply  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2018-11-02 12:28 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-01 20:23 Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
2018-11-01 20:32 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-11-01 20:50   ` Rich Shepard <rshepard@appl-ecosys.com>
2018-11-01 21:28     ` Tim Cross <theophilusx@gmail.com>
2018-11-01 20:34 ` David G. Johnston <david.g.johnston@gmail.com>
2018-11-01 20:53   ` Rich Shepard <rshepard@appl-ecosys.com>
2018-11-01 20:57     ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-11-01 20:58       ` Rich Shepard <rshepard@appl-ecosys.com>
2018-11-01 21:56 Re: Add columns to table; insert values based on row Rich Shepard <rshepard@appl-ecosys.com>
2018-11-02 00:08 ` Rich Shepard <rshepard@appl-ecosys.com>
2018-11-02 01:34   ` Ken Tanzer <ken.tanzer@gmail.com>
2018-11-02 12:28     ` Rich Shepard <rshepard@appl-ecosys.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox