September 7, 2015 at 10:31 am
declare @table table (
ParentID INT,
ChildID INT,
Value float
)
INSERT INTO @table
SELECT 1,1,1.2
union
select null,1,4.8
union
select null,1,4.6
union
select 2,2,1.8
union
select null,2,1.4
union
select 3,3,1.12
union
select null, 3 , 56.7
union
select null,3,43.6
select * from @table
/*
This case ParentID - Child 1 ,1 & 2,2 and 3,3 records are called as parent
where as null , 1 is child whoose parent is 1 similarly null,2 records are child whoose parent is 2 , .....
Now my requirement is to display parent records with value ascending and display next child records to the corressponding
parent and parent records are sorted ascending
*/
--Final output shuold be
PatentID ChildID VALUE
33 1.12
null3 56.7
null3 43.6
11 1.2
null1 4.8
null1 4.6
22 1.8
null1 1.4
September 7, 2015 at 11:27 am
ramrajan (9/7/2015)
declare @table table (ParentID INT,
ChildID INT,
Value float
)
INSERT INTO @table
SELECT 1,1,1.2
union
select null,1,4.8
union
select null,1,4.6
union
select 2,2,1.8
union
select null,2,1.4
union
select 3,3,1.12
union
select null, 3 , 56.7
union
select null,3,43.6
select * from @table
/*
This case ParentID - Child 1 ,1 & 2,2 and 3,3 records are called as parent
where as null , 1 is child whoose parent is 1 similarly null,2 records are child whoose parent is 2 , .....
Now my requirement is to display parent records with value ascending and display next child records to the corressponding
parent and parent records are sorted ascending
*/
--Final output shuold be
PatentID ChildID VALUE
33 1.12
null3 56.7
null3 43.6
11 1.2
null1 4.8
null1 4.6
22 1.8
null1 1.4
This does what you're looking for:
WITH cte AS
(
SELECT ChildID,
ROW_NUMBER() OVER (ORDER BY VALUE) AS RN1
FROM @table
WHERE ParentID IS NOT NULL
)
SELECT t1.*
FROM @table t1
JOIN cte t2 ON t1.ChildID = t2.ChildID
ORDER BY t2.RN1, t1.ParentID DESC, t1.Value DESC;
Results:
ParentID ChildID Value
----------- ----------- ----------------------
3 3 1.12
NULL 3 56.7
NULL 3 43.6
1 1 1.2
NULL 1 4.8
NULL 1 4.6
2 2 1.8
NULL 2 1.4
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 7, 2015 at 3:38 pm
ramrajan (9/7/2015)
declare @table table (ParentID INT,
ChildID INT,
Value float
)
INSERT INTO @table
SELECT 1,1,1.2
union
select null,1,4.8
union
select null,1,4.6
union
select 2,2,1.8
union
select null,2,1.4
union
select 3,3,1.12
union
select null, 3 , 56.7
union
select null,3,43.6
select * from @table
/*
This case ParentID - Child 1 ,1 & 2,2 and 3,3 records are called as parent
where as null , 1 is child whoose parent is 1 similarly null,2 records are child whoose parent is 2 , .....
Now my requirement is to display parent records with value ascending and display next child records to the corressponding
parent and parent records are sorted ascending
*/
--Final output shuold be
PatentID ChildID VALUE
33 1.12
null3 56.7
null3 43.6
11 1.2
null1 4.8
null1 4.6
22 1.8
null1 1.4
Not sure what this is for but that looks to have at least 1 major flaw in the idea of a parent/child table including but not limited to what a parent is and what a child is.
With that in mind, what is this for?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply