Advertisement

04.08.2008 at 08:19AM PDT, ID: 23304984
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

CTE recursive query question

Asked by jturkington in SQL Server 2005, MS SQL Server, SQL Server 2008

Tags: , ,

I have the following tables

sectors table
sectorid    int    PK
sector      varchar(50)
parentid   int       (if parent sector null value is inserted)

example data
sectorid        sector         parentid
1                   IT                 <null>
2                  Engineering  <null>
3                  Programming    1
4                  asp.net             3

jobs table
jobid     int   PK
jobtitle  varchar(50)
sectoridfk int  (FK to sectors table)

example data
jobid      jobtitle             sectoridfk
1           programmer     4 etc..

What i want to achieve is to bring back all top level sectors (ie.. where parentid is null) and display a total count of how many jobs they are associated with (including all their child sectorids).

Display Data: -
Engineering (0)
IT (1)
etc..

Trying to think of the best way to tackle this, maybe using sometype of CTE recursive query but can't think how to bring it all together in the one query ??

I have a basic cte soo far that only takes in one sectorid and determines all its child sectorids which could be used to perform a join on the jobs table ??: -

WITH SectorHierarchy (sectorId) AS
(
   -- Base case
   SELECT      sectorId
   FROM      Sectors
   WHERE      sectorId  = @sectorParentId

   UNION ALL

   -- Recursive step
   SELECT      s.sectorId
   FROM      sectors s
   INNER JOIN SectorHierarchy sh
      ON s.parentId = sh.sectorId
)

Help !!

JT


Start Free Trial
 
Loading Advertisement...
 
[+][-]04.08.2008 at 08:32AM PDT, ID: 21306259

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 08:52AM PDT, ID: 21306463

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 09:12AM PDT, ID: 21306728

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 09:35AM PDT, ID: 21306970

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 10:34AM PDT, ID: 21307614

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, MS SQL Server, SQL Server 2008
Tags: microsoft, sql server, 2005
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.08.2008 at 02:30PM PDT, ID: 21309853

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.08.2008 at 04:32PM PDT, ID: 21310581

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 11:00AM PDT, ID: 22491238

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_2_20070628