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 6067419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:36:21+00:00 2026-05-23T09:36:21+00:00

Running Postgres 7.4 (yeah we are upgrading) and I have a table where we

  • 0

Running Postgres 7.4 (yeah we are upgrading) and I have a table where we are merging three fields into a new field if the field has value.

Example Data:

record_id | old_field_1 | old_field_2 | old_field_3 | new_field
+---------+-------------+-------------+-------------+----------+
12345     | blah        | foo         |             |            <-- new_field = foo
12346     |             |             | what        |            <-- new_field = what
12347     |             |             |             |            <-- new_field IS NULL
12348     | hello       |             |             |            <-- new_field = hello

So this works below for one record:

UPDATE db_table
SET new_field = CASE 
    WHEN old_field_3 IS NOT NULL THEN old_field_3
    WHEN old_field_2 IS NOT NULL THEN old_field_2
    WHEN old_field_1 IS NOT NULL THEN old_field_1
    ELSE new_field
END
WHERE record_id = 123456

And the next record would be

UPDATE db_table
SET new_field = CASE 
    WHEN old_field_3 IS NOT NULL THEN old_field_3
    WHEN old_field_2 IS NOT NULL THEN old_field_2
    WHEN old_field_1 IS NOT NULL THEN old_field_1
    ELSE new_field
END
WHERE record_id = 123457

old_field_# (1,2,3) are all different values. If old_field_3 IS NOT NULL use this value for the new_field, else check the next value. But each record has it’s own unique value that needs to be updated

But I wanted to iterate over all the records in the database.

Trying a FOR loop but the syntax or proper usage I’m not implementing correctly. Just trying to get the SELECT working before trying to do the UPDATE syntax but having some issues.

FOR a_record IN SELECT * FROM db_table LOOP
    RAISE NOTICE 'record_id - %', a_record.record_id;
    RAISE NOTICE 'old_field_1     - %', a_record.old_field_1; 
END LOOP;

What am I missing?

  • 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-05-23T09:36:21+00:00Added an answer on May 23, 2026 at 9:36 am

    If i understand you correctly, if you want to do that combination for all records in a table, simply remove your where clause from your query.

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

Sidebar

Related Questions

I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
I have a Rails + Apache2 + Postgres + Passenger application running in production
I have a database (running on postgres, precisely) , with the following structure :
I'm running Perl 5.10.0 and Postgres 8.4.3, and strings into a database, which is
Running Rails 3.0.7 on Mac OS X with Ruby 1.9.2 I have three models
I have a postgres database with a table that contains rows I want to
I have a rails application running over Postgres. I have two servers: one for
I have postgresql running (/opt/local/lib/postgresql90/bin). The data base is set up @ /Users/demet8/postgres/data. I
i have a postgres database with millions of rows in it it has a
I am currently running postgres 8.4.4 and I have the need to override calls

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.