Bob Butcher
asked on
Join question in SQL Server - create statement
I have a table called tblcategory. It has all of my categories like below:
tblcategory
catnumber category
1 AS400
2 Network
3 Telecommuincations
my second table is subcategories. This is like so:
tblsubcategory
scatnumber scatname
1 User
2 Printer
3 Password
4 Program
5 Software
my last table is the link between the 2. It is called tblLinkCategory. It contains all of the subcategories per a category. So it is set like this:
tblLinkCategory
lnkcatnumber (which is the category) lnkscatnumber (which is the subcategory)
1 1
1 4
2 2
2 3
3 5
What I'd like to do is get a list - with a sql stmt - and read the tbllinkcategory and show me, by name what subcategories are found for a particular category. I struggle with joins so I'm not sure where to start.
I'd also like the sql stmt to show be the subcategories that weren't found connected to a particular category as well. I am using t-sql.
Thanks so much in advance.
tblcategory
catnumber category
1 AS400
2 Network
3 Telecommuincations
my second table is subcategories. This is like so:
tblsubcategory
scatnumber scatname
1 User
2 Printer
3 Password
4 Program
5 Software
my last table is the link between the 2. It is called tblLinkCategory. It contains all of the subcategories per a category. So it is set like this:
tblLinkCategory
lnkcatnumber (which is the category) lnkscatnumber (which is the subcategory)
1 1
1 4
2 2
2 3
3 5
What I'd like to do is get a list - with a sql stmt - and read the tbllinkcategory and show me, by name what subcategories are found for a particular category. I struggle with joins so I'm not sure where to start.
I'd also like the sql stmt to show be the subcategories that weren't found connected to a particular category as well. I am using t-sql.
Thanks so much in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT
lnkcatnumber --catnumber
,lnkscatnumber --subcatnumber
,cat.Category as CategoryName
,sub.scatName as SubCategoryName
FROM
tblLinkCategory lnk
INNER JOIN tblcategory cat ON lnk.lnkcatnumber = cat.catnumber
INNER JOIN tblsubcategory sub ON lnk.lnkscatnumber = sub.scatnumber
lnkcatnumber --catnumber
,lnkscatnumber --subcatnumber
,cat.Category as CategoryName
,sub.scatName as SubCategoryName
FROM
tblLinkCategory lnk
INNER JOIN tblcategory cat ON lnk.lnkcatnumber = cat.catnumber
INNER JOIN tblsubcategory sub ON lnk.lnkscatnumber = sub.scatnumber
FYI, the typical / "standard" naming for such relationship tables is to use both main table names/entities together, rather than "link".
CategorySubcategory
That inherently implies the link between them.
Or, if you absolutely insist:
tblCategorySubcategory
[Most people long, long ago dropped the "tbl" prefix, in galaxies both near and far, far away.]
Naming the actual link is much more useful than just saying "link". Also, a single table can link out to several other tables, so "link" is inadequate anyway.
CategorySubcategory
That inherently implies the link between them.
Or, if you absolutely insist:
tblCategorySubcategory
[Most people long, long ago dropped the "tbl" prefix, in galaxies both near and far, far away.]
Naming the actual link is much more useful than just saying "link". Also, a single table can link out to several other tables, so "link" is inadequate anyway.
ASKER
Thanks for the help -
>>I'd also like the sql stmt to show be the subcategories that weren't found connected to a particular category as well. I am using t-sql. <<
In your example all of the categories and all of the subcategories show up in the tbllinkcategory table, so each subcategory ties to a category. Perhaps you can provide some more data that covers all of the possible criteria you need to meet and your expected output. I can envision output that shows all subcategories with nulls where no category is met, or another output that shows all the categories with an aggregrated string of the subcategories which would be null if none existed, or still another output with a column for the category and columns for each subcategory and nulls where no matches exist, and there are probably other possibilities as well.
In your example all of the categories and all of the subcategories show up in the tbllinkcategory table, so each subcategory ties to a category. Perhaps you can provide some more data that covers all of the possible criteria you need to meet and your expected output. I can envision output that shows all subcategories with nulls where no category is met, or another output that shows all the categories with an aggregrated string of the subcategories which would be null if none existed, or still another output with a column for the category and columns for each subcategory and nulls where no matches exist, and there are probably other possibilities as well.
here is a nice visual presentation of joins:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
:)
Giannis