Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:17:47+00:00 2026-06-10T12:17:47+00:00

I use PostgreSQL 9.1.2 and I have a basic table as below, where I

  • 0

I use PostgreSQL 9.1.2 and I have a basic table as below, where I have the Survival status of an entry as a boolean (Survival) and also in number of days (Survival(Days)).

I have manually added a new column named 1-yr Survival and now I want to fill in the values of this column for each entry in the table, conditioned on that entry’s Survival and Survival (Days) column values. Once , completed the database table would look something like this:

Survival    Survival(Days)    1-yr Survival
----------  --------------    -------------
Dead            200                NO
Alive            -                 YES
Dead            1200               YES

The pseudo code to input the conditioned values of 1-yr Survival would be something like:

ALTER TABLE mytable ADD COLUMN "1-yr Survival" text
for each row
if ("Survival" = Dead & "Survival(Days)" < 365) then Update "1-yr Survival" = NO
else Update "1-yr Survival" = YES
end 

I believe this is a basic operation however I failed to find the postgresql syntax to execute it. Some search results return “adding a trigger”, but I am not sure that is what I neeed. I think my situation here is a lot simpler. Any help/advice would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:17:48+00:00Added an answer on June 10, 2026 at 12:17 pm

    The one-time operation can be achieved with a plain UPDATE:

    UPDATE tbl
    SET    one_year_survival = (survival OR survival_days >= 365);
    

    I would advise not to use camel-case, white-space and parenthesis in your names. While allowed between double-quotes, it often leads to complications and confusion. Consider the chapter about identifiers and key words in the manual.

    Are you aware that you can export the results of a query as CSV with COPY?
    Example:

    COPY (SELECT *, (survival OR survival_days >= 365) AS one_year_survival FROM tbl)
    TO '/path/to/file.csv';
    

    You wouldn’t need the redundant column this way to begin with.


    Additional answer to comment

    To avoid empty updates:

    UPDATE tbl
    SET    "Dead after 1-yr" = (dead AND my_survival_col < 365)
          ,"Dead after 2-yrs" = (dead AND my_survival_col < 730)
    ....
    WHERE  "Dead after 1-yr" IS DISTINCT FROM (dead AND my_survival_col < 365)
       OR  "Dead after 2-yrs" IS DISTINCT FROM (dead AND my_survival_col < 730)
    ...
    

    Personally, I would only add such redundant columns if I had a compelling reason. Normally I wouldn’t. If it’s about performance: are you aware of indexes on expressions and partial indexes?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Postgresql + PHP. Say I have this table: Books ( id, title,
I use PostgreSQL database and in one table I have the datetime column edit_user
I use Postgresql 9.1. I have a table and I would like to get
I have a large readonly Wordnet PostgreSQL database that I'd like to use from
say I have a postgresql table with the following values: id | value ----------
I want to use PostgreSQL locally since I have to use it on Heroku.
I have some kind of (basic) logging with user actions in a postgreSQL database.
I must use PostgreSQL but I have a little problem when I try to
I am having trouble setting up ejabberd to use PostgreSQL. My environment is OSX
Can I use MongoDB and a PostgreSQL in one rails app? Specifically I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.