0

I'd like to join 2 tables together. TableB has 15 distinct values that I want to go into the TableA (the one I'm inserting into). However, I also want to insert individual values for TableA. For example, I want to insert the 15 individual values from TableB into TableA, but I also want to be able to insert another field ('region') in TableA

 --so far I have this
 insert into TableA ((id)
      select distinct(id) from TableB
      group by id), region values('NYC')

I'm not sure how to insert the region in there...the above fails. I need to hardcore the regions in there because they are not in the other table.

3 Answers 3

3

You probably want something like this:

insert into TableA (id, region)
select distinct id, 'NYC'
from TableB
Sign up to request clarification or add additional context in comments.

Comments

0
INSERT INTO TableA (ID, REGION)
SELECT ID, 'NYC'
FROM TableB
GROUP BY ID

That's it.

The DISTINCT is redundant.

3 Comments

DISTINCT isn't necessarily redundant, e.g. if id is not a primary key.
The GROUP BY does indeed make it 100% redundant. I'd be intrigued to see duplicate id values when you group by id...
Ah, you're right there. I just went the other way and took out the GROUP BY
0

Try something like this:

insert into TableA (id, region)
    select distinct id, "NYC"
    from TableB
    group by id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.