Reg_ID User_Name Parent_ID 1 XYZ NULL 2 X 1 3 Y 1 4 Z 2 5 Z1 3 6 Z2 3 7 Y1 4 8 Y2 5
ALTER PROCEDURE [dbo].[P1] (@ID INT ) AS BEGIN SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE Reg_ID=@ID UNION ALL SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE Parent_ID=@ID UNION ALL SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE [Parent_ID]IN (SELECT Reg_ID FROM dbo.Child WHERE Parent_ID =@ID) END
ALTER PROCEDURE [dbo].[P1] (@ID INT ) AS BEGIN SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE Reg_ID=@ID UNION ALL SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE Parent_ID=@ID UNION ALL SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE [Parent_ID]IN (SELECT Reg_ID FROM dbo.Child WHERE Parent_ID=@ID) UNION ALL SELECT C.Reg_ID,c.USER_NAME FROM dbo.Child c WHERE Parent_ID IN (SELECT C.Reg_ID FROM dbo.Child c WHERE [Parent_ID]IN (SELECT Reg_ID FROM dbo.Child WHERE Parent_ID=@ID)) END
declare @tbl_accounts table (AccountID int, name varchar(50), ParentID int)
insert into @tbl_accounts select 1,'Alex',0 insert into @tbl_accounts select 2,'John',1 insert into @tbl_accounts select 3,'Mathew',2 insert into @tbl_accounts select 4,'Philip',1 insert into @tbl_accounts select 5,'Shone',0 insert into @tbl_accounts select 6,'Shine',2 insert into @tbl_accounts select 7,'Tom',2 insert into @tbl_accounts select 8,'George',1 insert into @tbl_accounts select 9,'Jim',5
;with cte as ( select Accountid, name, parentid, cast(row_number()over(partition by parentid order by name) as varchar(max)) as [path] from @tbl_accounts where parentid = 0 union all select t.AccountID, t.name, t.ParentID, [path] + cast(row_number()over(partition by t.parentid order by t.name) as varchar(max)) from cte join @tbl_accounts t on cte.AccountID = t.ParentID )
select Accountid, name, ParentID, [path] from cte order by path
WITH Hierarchy AS ( SELECT Parent_ID AS RootID , 1 AS [Level] , '\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] , Parent_ID , Child_ID FROM @Sample WHERE NOT Parent_ID IN ( SELECT Child_ID FROM @Sample ) UNION ALL SELECT P.RootID , P.[Level] + 1 , P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) , C.Parent_ID , C.Child_ID FROM Hierarchy P INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID ) SELECT * FROM Hierarchy ORDER BY [Path] ;
WITH Hierarchy AS ( SELECT Parent_ID AS RootID , 1 AS [Level] , '\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] , Parent_ID , Child_ID FROM @Sample WHERE NOT Parent_ID IN ( SELECT Child_ID FROM @Sample ) UNION ALL SELECT P.RootID , P.[Level] + 1 , P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) , C.Parent_ID , C.Child_ID FROM Hierarchy P INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID ) SELECT TOP 1 * FROM Hierarchy ORDER BY [Path] DESC ;
WITH Hierarchy AS ( SELECT Parent_ID AS RootID , 1 AS [Level] , '\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] , Parent_ID , Child_ID FROM @Sample WHERE NOT Parent_ID IN ( SELECT Child_ID FROM @Sample ) UNION ALL SELECT P.RootID , P.[Level] + 1 , P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) , C.Parent_ID , C.Child_ID FROM Hierarchy P INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID ) SELECT TOP 1 * FROM Hierarchy ORDER BY RootID DESC ;
SET @Auto=(SELECT RIGHT('0000000000'+ CONVERT(VARCHAR(50),MAX(SUBSTRING(Variation_Code,4,10))+1),10)AS Variation_Code FROM Inventory.ItemVariationDetails) SET @Auto='VAR'+ISNULL(@Auto,'0000000001')
------------------------------------------------------ WITH Hierarchy(RootID,Level,Path,Parent_ID,Child_ID) AS ( SELECT Parent_ID, 1, '\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX)), Parent_ID , Child_ID FROM Sample WHERE NOT Parent_ID IN ( SELECT Child_ID FROM Sample ) UNION ALL SELECT P.RootID , P.[Level] + 1 , P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) , C.Parent_ID , C.Child_ID FROM Hierarchy P INNER JOIN Sample C ON P.Child_ID = C.Parent_ID ), CTE(Parent_ID,Total_Child) AS(SELECT Parent_ID,COUNT(*) FROM Hierarchy GROUP BY Parent_ID )
SELECT TOP 1 RootID,Level,Path,P.Parent_ID,Child_ID,Total_Child FROM Hierarchy P JOIN CTE ON CTE.Parent_ID=P.Parent_ID ORDER BY RootID DESC ------------------------------------------------------------------
Reg_ID User_Name Parent_ID
ReplyDelete1 XYZ NULL
2 X 1
3 Y 1
4 Z 2
5 Z1 3
6 Z2 3
7 Y1 4
8 Y2 5
ALTER PROCEDURE [dbo].[P1]
(@ID INT
)
AS
BEGIN
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c WHERE Reg_ID=@ID
UNION ALL
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c WHERE Parent_ID=@ID
UNION ALL
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c
WHERE [Parent_ID]IN (SELECT Reg_ID FROM dbo.Child
WHERE Parent_ID =@ID)
END
1 a 0
Delete2 b 1
3 c 1
4 d 3
5 e 4
6 f 5
7 g 6
8 h 5
9 i 8
NULL NULL NULL
By passing id as 1 it should retrive all record becz they all are related to each other
ALTER PROCEDURE [dbo].[P1]
ReplyDelete(@ID INT
)
AS
BEGIN
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c WHERE Reg_ID=@ID
UNION ALL
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c WHERE Parent_ID=@ID
UNION ALL
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c
WHERE [Parent_ID]IN
(SELECT Reg_ID FROM dbo.Child WHERE Parent_ID=@ID)
UNION ALL
SELECT C.Reg_ID,c.USER_NAME FROM
dbo.Child c
WHERE Parent_ID IN (SELECT C.Reg_ID FROM
dbo.Child c
WHERE [Parent_ID]IN
(SELECT Reg_ID FROM dbo.Child WHERE Parent_ID=@ID))
END
hi
Deleteit have to come from 1 to 9 but it is only showing 1 to 5.keep coding:
Deletedeclare @tbl_accounts table (AccountID int, name varchar(50), ParentID int)
ReplyDeleteinsert into @tbl_accounts select 1,'Alex',0
insert into @tbl_accounts select 2,'John',1
insert into @tbl_accounts select 3,'Mathew',2
insert into @tbl_accounts select 4,'Philip',1
insert into @tbl_accounts select 5,'Shone',0
insert into @tbl_accounts select 6,'Shine',2
insert into @tbl_accounts select 7,'Tom',2
insert into @tbl_accounts select 8,'George',1
insert into @tbl_accounts select 9,'Jim',5
;with cte as
(
select
Accountid,
name,
parentid,
cast(row_number()over(partition by parentid order by name) as varchar(max)) as [path]
from @tbl_accounts
where parentid = 0
union all
select
t.AccountID,
t.name,
t.ParentID,
[path] + cast(row_number()over(partition by t.parentid order by t.name) as varchar(max))
from
cte
join @tbl_accounts t on cte.AccountID = t.ParentID
)
select
Accountid,
name,
ParentID,
[path]
from cte
order by path
Hi.......12:12 PM
ReplyDeleteDECLARE @Sample TABLE
ReplyDelete(
Parent_ID INT ,
Child_ID INT
) ;
INSERT INTO @Sample
VALUES ( 1, 2 ),
( 1, 3 ),
( 2, 4 ),
( 2, 5 ) ;
WITH Hierarchy
AS ( SELECT Parent_ID AS RootID ,
1 AS [Level] ,
'\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] ,
Parent_ID ,
Child_ID
FROM @Sample
WHERE NOT Parent_ID IN ( SELECT Child_ID
FROM @Sample )
UNION ALL
SELECT P.RootID ,
P.[Level] + 1 ,
P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) ,
C.Parent_ID ,
C.Child_ID
FROM Hierarchy P
INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID
)
SELECT *
FROM Hierarchy
ORDER BY [Path] ;
DECLARE @Sample TABLE
ReplyDelete(
Parent_ID INT ,
Child_ID INT
) ;
INSERT INTO @Sample
VALUES ( 1, 2 ),
( 1, 3 ),
( 2, 4 ),
( 2, 5 ) ;
WITH Hierarchy
AS ( SELECT Parent_ID AS RootID ,
1 AS [Level] ,
'\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] ,
Parent_ID ,
Child_ID
FROM @Sample
WHERE NOT Parent_ID IN ( SELECT Child_ID
FROM @Sample )
UNION ALL
SELECT P.RootID ,
P.[Level] + 1 ,
P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) ,
C.Parent_ID ,
C.Child_ID
FROM Hierarchy P
INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID
)
SELECT TOP 1 *
FROM Hierarchy
ORDER BY [Path] DESC ;
ok
DeleteDECLARE @Sample TABLE
ReplyDelete(
Parent_ID INT ,
Child_ID INT
) ;
INSERT INTO @Sample
VALUES ( 1, 2 ),
( 1, 3 ),
( 2, 4 ),
( 2, 5 ) ;
WITH Hierarchy
AS ( SELECT Parent_ID AS RootID ,
1 AS [Level] ,
'\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX))AS [Path] ,
Parent_ID ,
Child_ID
FROM @Sample
WHERE NOT Parent_ID IN ( SELECT Child_ID
FROM @Sample )
UNION ALL
SELECT P.RootID ,
P.[Level] + 1 ,
P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) ,
C.Parent_ID ,
C.Child_ID
FROM Hierarchy P
INNER JOIN @Sample C ON P.Child_ID = C.Parent_ID
)
SELECT TOP 1 *
FROM Hierarchy
ORDER BY RootID DESC ;
SET @Auto=(SELECT RIGHT('0000000000'+ CONVERT(VARCHAR(50),MAX(SUBSTRING(Variation_Code,4,10))+1),10)AS Variation_Code FROM Inventory.ItemVariationDetails)
ReplyDeleteSET @Auto='VAR'+ISNULL(@Auto,'0000000001')
hiiiiiiiiiiiiiii
DeleteCREATE TABLE Sample
ReplyDelete(
Parent_ID INT ,
Child_ID INT
)
INSERT INTO Sample
VALUES ( 1, 2 ),
( 1, 3 ),
( 2, 4 ),
( 2, 5 )
------------------------------------------------------
WITH Hierarchy(RootID,Level,Path,Parent_ID,Child_ID)
AS ( SELECT Parent_ID,
1,
'\\' + CAST(Parent_ID AS VARCHAR(MAX)) + '\' + CAST(Child_ID AS VARCHAR(MAX)),
Parent_ID ,
Child_ID
FROM Sample
WHERE NOT Parent_ID IN ( SELECT Child_ID
FROM Sample )
UNION ALL
SELECT P.RootID ,
P.[Level] + 1 ,
P.[Path] + '\' + CAST(C.Parent_ID AS VARCHAR(MAX)) ,
C.Parent_ID ,
C.Child_ID
FROM Hierarchy P
INNER JOIN Sample C ON P.Child_ID = C.Parent_ID
),
CTE(Parent_ID,Total_Child)
AS(SELECT Parent_ID,COUNT(*) FROM Hierarchy GROUP BY Parent_ID
)
SELECT TOP 1 RootID,Level,Path,P.Parent_ID,Child_ID,Total_Child
FROM Hierarchy P
JOIN CTE
ON CTE.Parent_ID=P.Parent_ID
ORDER BY RootID DESC
------------------------------------------------------------------
hi
ReplyDeleteSELECT * FROM [dbo].[aaaa]
ReplyDeleteORDER BY
CASE
WHEN ID >0 THEN (SELECT MAX(ID) FROM [dbo].[aaaa])+1
ELSE 0
END,
ID DESC