How to implement 'Lookup' in SQL Server Query

I have 2 tables which I want to query to populate a 3rd table with.
Table 1 contains about 1M rows of data.
Table 2 contains a lookup/conversion data i.e col 1 contains a code and col 2 contains meaning.
I am querying table 1 but using table 2 to convert where I find the code to the meaning

Table 2
col 1              col2
code 1           type 1
code 2           type 2
code 3           type 3


So I query table 1 INSERT INTO table 3 (col1, col2, col3, col4) FROM table 1 col 1 col2 (SELECT converted item FROM table 2)...

My problem is that I am getting mutiple rows from the sub query even though all the rows are unique.

What am I doing wrong?
LVL 16
Richard OlutolaConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you post full query and sample of the records you're getting and what should be expecting?
0
Richard OlutolaConsultantAuthor Commented:
INSERT INTO TABLE 3 (vref,sledg,tranxdt,ptp)
SELECT
 belnr,
(SELECT newcode FROM table 2 WHERE table2.oldcode = table1.oldcode),
 bldat,
 monat
FROM TABLE 1

Table 2 is just a lookup table.

Hope this helps.
Thanks
R.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can get rid of the subquery:
INSERT INTO TABLE 3 (vref,sledg,tranxdt,ptp)
SELECT t1.belnr, t2.newcode,  t1.bldat,  t1.monat
FROM TABLE1 t1
    INNER JOIN table2 t2 ON t2.oldcode = t1.oldcode

Open in new window

Without sample data I can't help much than this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard OlutolaConsultantAuthor Commented:
That did the trick.
I was close but didn't quite get there.
Many thanks for the quick response too.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.