Note that there are some explanatory texts on larger screens.

plurals
  1. POsql query distinct join
    primarykey
    data
    text
    <p>Sorry I missed and deleted earlier question on accident again. </p> <p>I have a situation, I am trying to select distinct values from table 1 that are new and store them in table 2. The problem is that table has duplicates on column "name" but it does have a key column "id", but the different ids of course map to the same name.</p> <p>My idea on the query would be</p> <pre><code>INSERT INTO TABLE2 (NAME, UniqueID) SELECT DISTINCT TABLE1.NAME, TABLE1.ID FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.ID=TABLE2.UniqueID WHERE TABLE2.NAME IS NULL </code></pre> <p>Need help on getting the query to return my desired results, right now it still produces duplicates in table2 (on name column), which I don't want. I would want it to only append new records even if I run the query multiple times. For example if two new records were added into table1 but one has the name already in table 2, then the query would only add 1 new record to table2</p> <p>just a note: I am using ms access, so it has strict syntax on single queries</p> <p><strong>EDIT:</strong></p> <p>Folliwing input I had came with this query</p> <pre><code>INSERT INTO TABLE2 (NAME, UniqueID) SELECT TABLE1.NAME, Min(TABLE1.ID) FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.NAME=TABLE2.NAME WHERE TABLE2.UniqueID IS NULL Group By TABLE1.NAME; </code></pre> <p>but these actually had to be separated to two separate wueries in access to run without a reserver error flag but now I ran into additional problem. When I run the two separate queries, it works fine the first time, but when I run it twice trying to test to see if any new records have been added to table 1, it then appends 1 record when no new records are in table 1, so it appends a blank name value and a duplicate unique id, and continually does that same process everytime I run it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload