[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Getting recursive values of sub records in MSSQL 2005

Posted on 2007-11-14
4
Medium Priority
?
232 Views
Last Modified: 2011-09-20
I have a table called "categories" which contains all categories of "widgets".  Categories can be "sub_of" as well.  There is a field.  The structure looks like.

category_id int id
category_name varchar (50)
sub_of int

The data look like this

category_id             category_name                        sub_of
--------------------------------------------------------------------------
1                             Widgets                                        0   -  this is the root.
2                             Nice Widgets                                 1
3                             Cool Widgets                                1
4                             Very Cool widgets                         3

In my "widgets" table there is reference (fk) to category_id to categorize the widgets.

Question - I need to create a query to quickly return the contents and counts in each category.

For example, if I had 10 widgets total, the counts would look like:

>Widgets (10)
    >Nice Widgets (5)
    >Cool Widgets (5)   (note: 3 in this category and two below).
        > Very Cool Widgets (2)

So if I ask for all Widgets in "widgets" I get all widgets not only spefically in that category, but in all sub categories as well.  I also need the counts in similar manner.

I need actual code examples please.

Thank you.
0
Comment
Question by:ccleebelt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20282542
the hierarchical part is easy, using the CTE method in SQL 2005...
did you achieve that already?
0
 

Author Comment

by:ccleebelt
ID: 20282684
no - I am just getting this started.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20283312
ok, lets' see to get part 1 done, the recursion without the counts yet
http://msdn2.microsoft.com/en-us/library/ms186243.aspx
0
 

Author Comment

by:ccleebelt
ID: 20283498
actually, this does it...thanks.  I can just add the count(*) to the last query and go from there.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

656 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