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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:35:36+00:00 2026-05-30T01:35:36+00:00

POSTGRES- I want update the Employees.zipcode_mod column in the Employees table for the ‘invalid

  • 0

POSTGRES-
I want update the Employees.zipcode_mod column in the Employees table for the ‘invalid zipcodes’ (Employees.zipcode) which are invalid if they do NOT EXIST in Ref_Zips.zip5

The update rule is to find all the invalid zipcodes that are 3 chars or long and match them on first three digits of Tmp_Agg_Zips.zip column and update Employees.zipcode_mod with Tmp_Agg_Zips.zip that has the highest number Tmp_Agg_Zips.emp_cnt. If there is a tie between multiple Tmp_Agg_Zips.zip values, then get the ‘highest’ zip value.

Update

If the invalid zipcode is over 3 chars but its first three digits do not match any of the first three digits of Tmp_Agg_Zips.zip OR invalid zipcode is less than 3 chars or null, then just update Employees.zipcode_mod with Tmp_Agg_Zips.zip that has the maximum value of Tmp_Agg_Zips.emp_cnt, irrespective of the first three digits. Ex- 88888 and null are updated to 10012 in the example below.

This is for Postgres 8.4.

Employees

Gender | zipcode | zipcode_mod
   M   |  99574  |
   F   |  99574  |
   F   |  10012  |
   F   |  10012  |
   F   |  10012  |
   F   |  19001  |
   M   |    100  | 10012
   M   |    190  | 19001
   M   |     19  | 10012
   F   |   null  | 10012
   F   |  88888  | 10012
   F   |   8888  | 10012

Tmp_Agg_Zips

  zip | emp_cnt
99574 |  2
10012 |  3
19001 |  1

Ref_Zips

zip5
99574
10012
19001
  • 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-30T01:35:38+00:00Added an answer on May 30, 2026 at 1:35 am

    For updated question

    I added a COALESCE() clause to catch the cases where no matching alternative is found. And put the computation of the default value into a subquery for multiple use.

    UPDATE employees e
    SET    zipcode_mod =
       CASE WHEN length(e.zipcode) > 2 THEN
          COALESCE((
             SELECT t.zip
             FROM   tmp_agg_zips t
             WHERE  substr(t.zipcode, 1, 3) = substr(e.zipcode, 1, 3)
             ORDER  BY t.emp_cnt DESC, t.zip  -- lowest zip for mult. emp_cnt
             LIMIT  1
             ), t0.zip)
       ELSE
          t0.zip
       END
    FROM  (
       SELECT zip
       FROM   tmp_agg_zips
       ORDER  BY emp_cnt DESC, t.zip
       LIMIT  1
       ) t0
    WHERE  NOT EXISTS (
       SELECT *
       FROM   ref_zips r
       WHERE  r.zip5 = e.zipcode
       )
    

    For original question

    This query works with older versions of PostgreSQL:

    UPDATE employees e
    SET    zipcode_mod =
        CASE WHEN length(e.zipcode) > 2 THEN (
            SELECT t.zip
            FROM   tmp_agg_zips t
            WHERE  substr(t.zipcode, 1, 3) = substr(e.zipcode, 1, 3)
            ORDER  BY t.emp_cnt DESC, t.zip -- lowest zip for mult. emp_cnt
            LIMIT  1
            )
        ELSE (
            SELECT zip
            FROM   tmp_agg_zips
            ORDER  BY emp_cnt DESC, t.zip
            LIMIT  1
            )
        END
    WHERE  NOT EXISTS (
            SELECT *
            FROM   ref_zips r
            WHERE  r.zip5 = e.zipcode
            )
    

    In PostgreSQL 9.1, a CTE should perform better:

    WITH x AS (
        SELECT zip
        FROM   tmp_agg_zips
        ORDER  BY emp_cnt DESC, t.zip
        LIMIT  1
        )
    UPDATE employees e
    SET    zipcode_mod =
        CASE WHEN length(e.zipcode) > 2 THEN (
            SELECT t.zip
            FROM   tmp_agg_zips t
            WHERE  left(t.zipcode, 3) = left(e.zipcode, 3)
            ORDER  BY t.emp_cnt DESC, t.zip  -- pick lowest zip
            LIMIT  1
            )
        ELSE
            x.zip
        END
    FROM   x
    WHERE  NOT EXISTS (
            SELECT *
            FROM   ref_zips r
            WHERE  r.zip5 = e.zipcode
            )
    

    If there are multiple rows in tmp_agg_zips with the same (highest) emp_cnt, I pick the “lowest” zip. You did not specify how to break these ties.

    BTW, different column names for zip codes are not helpful for me. Table-qualifying the column names does a better job.

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

Sidebar

Related Questions

I want sql script of postgres 9 database schema which is not on local
I have a pretty large table (20M records) which has a 3 column index
I did some changes to a postgres table and I want to revert it
I have a postgres database with a table that contains rows I want to
I create a slq table (postgres db) and i want to print all value
I have the following table in postgres: CREATE TABLE test ( id serial NOT
I want to create a postgres function that builds the set of columns it
I want to remove duplicate rows return from a SELECT Query in Postgres I
postgres has an array data type, in this case a numeric array: CREATE TABLE
I have a postgres database with a user table (userid, firstname, lastname) and a

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.