6

I am working in Postgres 9.4. I have a table that looks like this:

     Column      │         Type         │                             Modifiers
─────────────────┼──────────────────────┼───────────────────────
 id              │ integer              │ not null default 
 total_list_size │ integer              │ not null
 date            │ date                 │ not null
 pct_id          │ character varying(3) │

I want to take all values where date='2015-09-01', and create identical new entries with the date 2015-10-01.

How can I best do this?

I can get the list of values to copy with SELECT * from mytable WHERE date='2015-09-01', but I'm not sure what to do after that.

1
  • 1
    Is id column serial? Commented Feb 5, 2016 at 11:02

1 Answer 1

5

If the column id is serial then

INSERT INTO mytable (total_list_size, date, pct_id)
SELECT total_list_size, '2015-10-01', pct_id
FROM mytable 
WHERE date = '2015-09-01';

else, if you want the ids to be duplicated:

INSERT INTO mytable (id, total_list_size, date, pct_id)
SELECT id, total_list_size, '2015-10-01', pct_id
FROM mytable 
WHERE date = '2015-09-01';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.