Solved

derived query

Posted on 2007-04-09
5
285 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

636 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