Solved

Join question in SQL Server - create statement

Posted on 2013-01-28
6
298 Views
Last Modified: 2013-01-28
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.
0
Comment
Question by:samic400
6 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 38827087
Hi,

Try the following for search with category name:

DECLARE @CategoryName VARCHAR(50)
SET @CategoryName = 'AS400' --Or whatever you like

SELECT  C.category,
		SC.scatnam
FROM	tblcategory C
		JOIN tblLinkCategory LC
			ON C.catnumber = LC.lnkcatnumber
		JOIN tblsubcategory SC
			ON SC.scatnumber = LC.lnkscatnumber
WHERE	C.category = @CategoryName

Open in new window


or for search with category number

DECLARE @CategoryNumber INT
SET @CategoryNumber = 1 --Or whatever you like

SELECT  C.category,
		SC.scatnam
FROM	tblcategory C
		JOIN tblLinkCategory LC
			ON C.catnumber = LC.lnkcatnumber
		JOIN tblsubcategory SC
			ON SC.scatnumber = LC.lnkscatnumber
WHERE	C.catnumber = @CategoryNumber

Open in new window


and for search of sub categories with no master categories:

SELECT  SC.scatnumber,
		SC.scatnam
FROM	tblsubcategory SC
		LEFT JOIN tblLinkCategory LC
			ON SC.scatnumber = LC.lnkscatnumber
		LEFT JOIN tblcategory C
			ON C.catnumber = LC.lnkcatnumber
WHERE	C.catnumber IS NULL

Open in new window


Regards,
Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38827109
By the way,  

here is a nice visual presentation of joins:

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

:)

Giannis
0
 
LVL 6

Expert Comment

by:esolve
ID: 38827125
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 38827357
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.
0
 
LVL 13

Author Closing Comment

by:samic400
ID: 38827563
Thanks for the help -
0
 
LVL 31

Expert Comment

by:awking00
ID: 38828236
>>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.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL server 2008 SP4 29 34
t-sql month question 8 43
Date conversion in sql server 2012 6 26
BULK INSERT most recent CSV 19 20
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now