Solved

derived query

Posted on 2007-04-09
5
282 Views
Last Modified: 2008-02-01
Hi,
I have a table CH in SQL Server 2005 with columns ParentID (PK) and childID. ChildID could be the ParentID of Another ChildID. I want all the ChildID that are also the ParentIDs.

I have tried this:
Select ParentID From  (Select ChildID from ch) ch
   
But it does not work.
0
Comment
Question by:SA4
[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
5 Comments
 
LVL 9

Accepted Solution

by:
gpompe earned 250 total points
ID: 18878502
Try this
select a1.ChildID from Ch a1 join Ch a2 on a1.ChildId=a2.ParentID
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18878510
please try this:

WITH R ( ParentID, ChildID, Level )
AS (
   SELECT ParentID, ChildID, 0
   FROM CH
   WHERE ParentID IS NULl
UNION ALL
   SELECT c.ParentID, c.ChildID, R.Level + 1
   FROM R
   JOIN CH
     ON R.ChildID = CH.ParentID
   )
SELECT *
FROM R

http://www.eggheadcafe.com/articles/sql_server_recursion_with_clause.asp
0
 

Author Comment

by:SA4
ID: 18878786
thanks for the reply, I tried this:
WITH R ( ParentID, ChildID, Level )
AS (
   SELECT ParentID, ChildID, 0
   FROM CH
   WHERE ParentID IS NULl
UNION ALL
   SELECT c.ParentID, c.ChildID, R.Level + 1
   FROM R
   JOIN CH
     ON R.ChildID = CH.ParentID
   )
SELECT *
FROM R

But It does not give any result And I know that there are a few that I should be getting.
 
0
 
LVL 25

Assisted Solution

by:jrb1
jrb1 earned 250 total points
ID: 18879366
select parentid from ch where parentid in (select cihldid from ch)
0
 

Author Comment

by:SA4
ID: 18883354
thanks for your help.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

739 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