Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Parent Child Relationship

Author  Topic 

brindaam
Starting Member

4 Posts

Posted - 2010-12-13 : 08:35:21
Hi,

I have two queries, say for example

Select distinct 0 parent_id, ROW_NUMBER() OVER(
ORDER BY Column2_Desc ) AS 'ID', Column2_Desc
From table1
Union
Select distinct 0 parent_id, ROW_NUMBER() OVER(
ORDER BY Column2_Desc ) AS 'ID', Column2_Desc
From table2

I don’t have relationship between two tables.But I want to frame parent child relationship between these two so that my resultset should like

Parent_id id Column2_Desc
0 1 ABC
0 2 XYZ
0 3 ASDF
1 4 VGTY
2 5 DFT

I don’t want to use stored procedure.
Kindly Help Me Out



Brinda

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-12-13 : 09:04:11
Can you post the table structure and some sample data ?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-12-13 : 09:25:09
Why don't you want to use a stopred procedure or a view (a guess)

If there is no relationship, do you have a rule that you would use to tell how the data realtes?

This DB 101 stuff, and it's important to understand



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

brindaam
Starting Member

4 Posts

Posted - 2010-12-14 : 00:10:48
Hi,
This is what the structure of my table

Row_id column2_desc column3
1 ABC XYZ
2 ASDF ZSER

quote:
Originally posted by pk_bohra

Can you post the table structure and some sample data ?



Brinda
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-12-14 : 00:18:26
can you post the sample data together with the corresponding expected result ?

what is the relationship between table 1 and 2 ? And what is the parent - child relationship here ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

brindaam
Starting Member

4 Posts

Posted - 2010-12-14 : 00:20:18
Hi,
i have included the data also

quote:
Originally posted by khtan

can you post the sample data together with the corresponding expected result ?


KH
[spoiler]Time is always against us[/spoiler]





Brinda
Go to Top of Page

brindaam
Starting Member

4 Posts

Posted - 2010-12-14 : 02:41:38
Hi ,
I have a Table as below,

ID Column1 Column2 Column3
1 ABC ZXC AAA
2 123 ASDF BBB
3 DERT ASDER ASDF

And I need the output like this

ID PARENTID HEADERTEXT
1 NULL ABC
2 NULL 123
3 NULL DERT
4 1 ZXC
5 2 ASDF
6 3 ASDER
7 4 AAA
8 5 BBB
9 6 ASDF

quote:
Originally posted by khtan

can you post the sample data together with the corresponding expected result ?

what is the relationship between table 1 and 2 ? And what is the parent - child relationship here ?


KH
[spoiler]Time is always against us[/spoiler]





Brinda
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-12-14 : 16:28:57
[code]DECLARE @Sample TABLE
(
ID INT,
Column1 VARCHAR(20),
Column2 VARCHAR(20),
Column3 VARCHAR(20)
)

INSERT @Sample
SELECT 1, 'ABC', 'ZXC', 'AAA' UNION ALL
SELECT 2, '123', 'ASDF', 'BBB' UNION ALL
SELECT 3, 'DERT', 'ASDER', 'ASDF'

SELECT ID,
NULL AS PARENTID,
Column1 AS HEADERTEXT
FROM @Sample

UNION ALL

SELECT ID + COUNT(*) OVER (),
ID,
Column2
FROM @Sample

UNION ALL

SELECT ID + 2 * COUNT(*) OVER (),
ID + COUNT(*) OVER (),
Column3
FROM @Sample[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -