Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 complicated join

Author  Topic 

neil_akoga
Yak Posting Veteran

56 Posts

Posted - 2011-06-13 : 11:12:31
ok, i've done this before but i can't think of how i did it last time. i have 4 tables as illustrated below. what i want to do is create the desired results table shown at the bottom. sorry if this isn't that complicated but i'm having a total brainfart today


prodtable
itemcode / cat1 / cat2 / cat3
123 / 1 / 2 / 3
124 / 1 / 2 /
125 / 1 / 3 /
126 / 1 / /

cat1table
cat1 / cat1name
1 / meat

cat2table
cat2 / cat2name
2 / steaks
3 / burgers

cat3table
cat3 / cat3name
3 / sirloin


desired results table
itemcode / cat1name / cat2name / cat3name
123 / meat / steaks / sirloin
124 / meat / steaks /
125 / meat / burgers /
126 / meat / /

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-13 : 12:55:47
select t.itemcode, t1.cat1name, t2.cat2name, t3.cat3name
from prodtable t
left join cat1table t1
on t.cat1 = t1.cat1
left join cat2table t2
on t.cat2 = t2.cat2
left join cat3table t3
on t.cat3 = t1.cat3
order by t.itemcode

Why have 3 cat tables?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -