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 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
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
Again, get a copy of TREES & HIERARCHIES IN SQL and look up the Nested
Sets Model for trees. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) {
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
| |