473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

query to trace all parents

Hi,

I have a table with filled out below data:

+------+-----+
|parent|child|
+------+-----+
|A |B |
|B |C |
|B |E |
|C |D |
|E |F |
|E |G |
+------+-----+

So I have to make a query which get all 'parent' values values for
given child value.

For example :
-----------------
If I have to get all parent values for 'D' child., query must get this
values : C, B, A.

If I have to get all parent values for 'F' child., query must get this
values : E, B, A.

If I have to get all parent values for 'C' child., query must get this
values : B, A.

If I have to get all parent values for 'B' child., query must get this
values : A only.
-----------------

Is it possible to create a query which will covers all above conditions
or not using only sql statement without UDF or stored procedures.

Any solutiuons?

Sincerely,
Rustam Bogubaev

Jul 23 '05 #1
3 3517
PYCTAM (rb*******@bookinturkey.com) writes:
So I have to make a query which get all 'parent' values values for
given child value.

For example :
-----------------
If I have to get all parent values for 'D' child., query must get this
values : C, B, A.

If I have to get all parent values for 'F' child., query must get this
values : E, B, A.

If I have to get all parent values for 'C' child., query must get this
values : B, A.

If I have to get all parent values for 'B' child., query must get this
values : A only.
-----------------

Is it possible to create a query which will covers all above conditions
or not using only sql statement without UDF or stored procedures.


In SQL2000, no.

In SQL 2005, slated for release in November, there is support for
recursive queries.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
in this hopeless situation problem is solved using UDF :

CREATE FUNCTION getAllParents(
@child NVARCHAR(1)
) RETURNS @PARENTS TABLE (
[parent] NVARCHAR(1)
) AS BEGIN
DECLARE @parent NVARCHAR(1)

SELECT @parent = parent FROM table WHERE child = @child

WHILE @@ROWCOUNT = 1 BEGIN
INSERT @PARENTS SELECT @parent

SELECT @child = @parent
SELECT @parent = parent FROM table WHERE child = @child
END

RETURN
END

Jul 23 '05 #3
Again, get a copy of TREES & HIERARCHIES IN SQL and look up the Nested
Sets Model for trees.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dr zoidberg | last post by:
Hello, $a = mysql_query("Select a,b FROM t WHERE category=1") while($a) { $x = $a; //some echo //another query $b = mysql_query("Select * FROM t WHERE subcategory=$x") while ($b) {
0
by: John Macon | last post by:
------=_NextPart_000_04B4_01C36308.415CE100 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi all, Long time reader, first time poster, I hope...
4
by: srussell705 | last post by:
I have a 1:1 relationship between tables and am finding that the parent is sometimes mising the child. How do I query for what is not there? TIA
2
by: Lumpierbritches | last post by:
I have a table of Animals and their family heritage that I would like to populate when an unbound combobox pulls up the name of the Animal, the parents of that animal and their parents etc populate...
1
by: Lumpierbritches | last post by:
I'm trying to pull all the parents of a particular animal and I have my SQL statement that says not supported and when I attempt to run the Query, I get The SQL statement couldn't be executed...
3
by: Jim Lewis | last post by:
I have read several things that state accessing a Web Service through a Query String should work. However, when I try to execute http://localhost/webservice1/service1.asmx/HelloWorld I get the...
3
by: wjreichard | last post by:
OK ... I am using UPS Worldship that issues an ODBC query to my MS2K server ... Worldship can query either a table or a view and retreive shipping info for a supplied orderid. I need to create a...
1
by: Luqman | last post by:
I have created a Insert Query in Sql Data Source using Oracle Database, with the parameters, and its connected with DetailView Control. When I try to Insert through DetailView Control, Illegal...
13
by: eyalco | last post by:
I have a database of kindergarten in which there are parents and children of course. There are cases where a parent has few kids there. I built a query which colects the names and e-mails where I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.