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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get Last Parent Row

Author  Topic 

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-14 : 22:50:42
hi good day!

table

id details parent
1 this null
2 is 1
3 a sample 2
4 table 3


if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-14 : 23:50:56
If @param=4
Begin
Select * from TableName where ID=1
End


Is the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?

Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 00:27:53
quote:
Originally posted by MIK_2008

If @param=4
Begin
Select * from TableName where ID=1
End


Is the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?





sorry for bad english. i mean if the sql parameter is 4 how could i get the row which is row 1 that is the last parent of the list... :D thanks
Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 00:32:54
quote:
Originally posted by MIK_2008

If @param=4
Begin
Select * from TableName where ID=1
End


Is the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?






hi, even if the sql parameter is 3 or 2 it should return the row 1 because this is their last parent. i dont know how to call it. the first hierarchy of the parameter.. :D thanks again
Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 01:13:48
how to get the grandparent row... :d
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2011-02-15 : 01:37:56
This?



declare @t table (id int ,parent int)
insert @t
select 1,null union
select 2,1 union
select 3 ,2 union
select 4,3

select * from @t

;with cte
as
(

select id,parent,parent p from @t
union all
select t1.id,t2.id,t2.p from @t t1 inner join cte t2 on t1.parent=t2.id

)

select min(p)parent, id from cte where id=4 group by id




PBUH

Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 01:44:06
quote:
Originally posted by Sachin.Nand

This?



declare @t table (id int ,parent int)
insert @t
select 1,null union
select 2,1 union
select 3 ,2 union
select 4,3

select * from @t

;with cte
as
(

select id,parent,parent p from @t
union all
select t1.id,t2.id,t2.p from @t t1 inner join cte t2 on t1.parent=t2.id

)

select min(p)parent, id from cte where id=4 group by id




PBUH





whew... great! this is what i need... thanks a lot! more knowledge to you! will test on my table! thanks thanks!
Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 02:00:36
i got this error.. Outer join is not allowed in the recursive part of a recursive common table expression 'cte'.

because i have two tables... i have to get the parentid from another table..


Table1
id details
1 this
2 is
3 a sample
4 table

Table2
id parent
2 1
3 2
4 3


Sorry for not giving the complete details... just new with cte... thanks thanks

Go to Top of Page

bo0tl3ss
Starting Member

20 Posts

Posted - 2011-02-15 : 02:45:20
found it thanks...instead of using table1 to join i used table2! thanks a lot bro! it really helps! thanks
Go to Top of Page
   

- Advertisement -