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.
Author |
Topic |
khanewal
Starting Member
33 Posts |
Posted - 2010-08-24 : 04:09:36
|
Hi can plz help me outCan i get results 1 2 3 and 4 col b is ref id Using self joinId. Col A. Col B1. Abc. 02. Cde. 03. Ef. 14. Gh. 25. Ij. 0 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-24 : 07:46:48
|
didnt get that. can you elaborate what exactly are you looking at?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-08-24 : 08:15:50
|
You could mean so many things by that question.This gives you what you asked for but I'm not sure that's actually what you wanted.DECLARE @foo TABLE ( [ID] INT , [ColA] VARCHAR(3) , [colB] INT )INSERT @foo SELECT 1, 'Abc', 0UNION SELECT 2, 'Cde', 0UNION SELECT 3, 'Ef', 1UNION SELECT 4, 'Gh', 2UNION SELECT 5, 'Ij', 0; WITH heirarchy AS ( SELECT [ID], [colA], [colb] FROM @foo WHERE [colb] <> 0 UNION ALL SELECT f2.[ID], f2.[colA], f2.[colb] FROM @foo AS f2 JOIN heirarchy h ON h.[colB] = f2.[ID] )SELECT * FROM heirarchy ORDER BY [ID] Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
khanewal
Starting Member
33 Posts |
Posted - 2010-08-24 : 20:03:10
|
Hi Charlie,Thanks for your reply, |
 |
|
khanewal
Starting Member
33 Posts |
Posted - 2010-08-25 : 01:39:08
|
Query:SELECT [ID] AS ParentID, [parent_ID] AS ChildID,[theNote], [Page_ID] FROM dbo.main_notes WHERE [main_ID] = 39Output:Parent Child Page No28 0 4230 28 031 30 032 31 033 0 4334 33 035 34 038 33 0there is a query and output, but I need to insert page_ID number in the output like thatREQUIRED OUTPUT:Parent Child Page NO28 0 4230 28 4231 30 4232 31 4233 0 4334 33 4335 34 43 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-25 : 09:50:19
|
you need to create function like belowCREATE FUNCTION GetParentPage(@ID int)RETURNS intASBEGINDECLARE @Root int;WIth CTEAS(SELECT Parent,Child,PageNoFROM TableWHERE Parent=@IDUNION ALLSELECT t.Parent,t.Child,t.PageNoFROM CTE cJOIN Table tON t.Parent=c.Child)SELECT @Root=PageNo FROM CTEWHERE Child=0OPTION (MAXRECURSION 0)RETURN @RootENDthen call it likeSELECT parent,Child,dbo.GetParentPage(parent)FROM Table ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-08-25 : 15:14:45
|
Run this ...declare @temp table(id int, parent int, pag int)insert into @temp values(28, 0, 42)insert into @temp values(30, 28, 0)insert into @temp values(31, 30, 0)insert into @temp values(32, 31, 0)insert into @temp values(33, 0 ,43)insert into @temp values(34, 33, 0)insert into @temp values(35, 34, 0)insert into @temp values(38, 33, 0);with temp as(select id, parent, pag as page_id from @temp as t1 where parent = 0union all select t3.id,t3.parent, page_id from @temp as t3 inner join temp as t4 on t4.id = t3.parent)select * from temp order by id--- result id parent page_id28 0 4230 28 4231 30 4232 31 4233 0 4334 33 4335 34 4338 33 43 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-26 : 11:35:37
|
you can make it a derived column ([Page Number] in your example) and then use it like [Page Number]=somevalue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|