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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:44:46+00:00 2026-06-04T19:44:46+00:00

I’m having an Idiot Day today. I’m sure this is relatively simple, but my

  • 0

I’m having an Idiot Day today. I’m sure this is relatively simple, but my brain just isn’t giving me the answer.

I have a table whose rows are types of object. Looks something like this:

id    name    foo    bar    house_id
1     Cat     12     4      1
2     Cat     9      4      2
3     Dog     8      23     1
4     Bird    9      54     1
5     Bird    78     2      2
6     Bird    29     32     3

This isn’t how I’d choose to implement it, but it’s what I’m working with. Objects (cats, dogs and birds, in real life they’re actual business things) have been added to the table on an ad-hoc basis. When house_id 1 needs cats in it, a record for cats gets put in. When house_id 3 gets dogs, a record gets put in for dogs.

I now need to update this table so every type of object (Cat, Dog, Bird) has a record for a given house_id. I want to do this by inserting the result from a select query that returns a single record for each type, with the earliest values for ‘foo’ and ‘bar’ from a row of that type, if and only if there is no existent record for that type with the given house_id.

So for the above example data, where the given house_id = 3, the select query would return the following:

name    foo    bar    house_id
Cat     12     4      3
Dog     8      23     3

which I can then insert straight into the table.

Basically, return the first row of each distinct name if there are no rows of that name with a given house_id.

Suggestions welcome. DB engine is postgres if that helps.

  • 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-04T19:44:47+00:00Added an answer on June 4, 2026 at 7:44 pm
    SET search_path= 'tmp';
    
    DROP TABLE dogcat CASCADE;
    CREATE TABLE dogcat
            ( id serial NOT NULL
            , zname    varchar
            , foo    INTEGER
            , bar    INTEGER
            , house_id INTEGER NOT NULL
            , PRIMARY KEY (zname,house_id)
            );
    INSERT INTO dogcat(zname,foo,bar,house_id) VALUES
      ('Cat',12,4,1)
     ,('Cat',9,4,2)
     ,('Dog',8,23,1)
     ,('Bird',9,54,1)
     ,('Bird',78,2,2)
     ,('Bird',29,32,3)
            ;
    -- Carthesian product of the {name,house_id} domains
    WITH cart AS (
            WITH beast AS (
                    SELECT distinct zname AS zname
                    FROM dogcat
                    )
            , house AS (
                    SELECT distinct house_id AS house_id
                    FROM dogcat
                    )
            SELECT beast.zname AS zname
            ,house.house_id AS house_id
            FROM beast , house
            )
    INSERT INTO dogcat(zname,house_id, foo,bar)
    SELECT ca.zname, ca.house_id
            ,fb.foo, fb.bar
    FROM cart ca
         -- find the animal with the lowes id
    JOIN dogcat fb ON fb.zname = ca.zname AND NOT EXISTS
            ( SELECT * FROM dogcat nx
            WHERE nx.zname = fb.zname
            AND nx.id < fb.id
            )
    WHERE NOT EXISTS (
            SELECT * FROM dogcat dc
            WHERE dc.zname = ca.zname
            AND dc.house_id = ca.house_id
            )
            ;
    
    SELECT * FROM dogcat;
    

    Result:

    SET
    DROP TABLE
    NOTICE:  CREATE TABLE will create implicit sequence "dogcat_id_seq" for serial column "dogcat.id"
    NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "dogcat_pkey" for table "dogcat"
    CREATE TABLE
    INSERT 0 6
    INSERT 0 3
     id | zname | foo | bar | house_id 
    ----+-------+-----+-----+----------
      1 | Cat   |  12 |   4 |        1
      2 | Cat   |   9 |   4 |        2
      3 | Dog   |   8 |  23 |        1
      4 | Bird  |   9 |  54 |        1
      5 | Bird  |  78 |   2 |        2
      6 | Bird  |  29 |  32 |        3
      7 | Cat   |  12 |   4 |        3
      8 | Dog   |   8 |  23 |        2
      9 | Dog   |   8 |  23 |        3
    (9 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.