Click here to Skip to main content
15,908,843 members
Home / Discussions / Database
   

Database

 
AnswerRe: How to Sort this Data Pin
J4amieC16-Feb-10 2:38
J4amieC16-Feb-10 2:38 
GeneralRe: How to Sort this Data Pin
Vimalsoft(Pty) Ltd16-Feb-10 2:45
professionalVimalsoft(Pty) Ltd16-Feb-10 2:45 
AnswerRe: How to Sort this Data Pin
Richard MacCutchan16-Feb-10 2:54
mveRichard MacCutchan16-Feb-10 2:54 
QuestionRe: How to Sort this Data Pin
Chris Meech16-Feb-10 3:07
Chris Meech16-Feb-10 3:07 
AnswerRe: How to Sort this Data Pin
Vimalsoft(Pty) Ltd16-Feb-10 3:25
professionalVimalsoft(Pty) Ltd16-Feb-10 3:25 
AnswerRe: How to Sort this Data Pin
dan!sh 16-Feb-10 6:13
professional dan!sh 16-Feb-10 6:13 
AnswerRe: How to Sort this Data Pin
Jörgen Andersson16-Feb-10 9:06
professionalJörgen Andersson16-Feb-10 9:06 
GeneralRe: How to Sort this Data Pin
Vimalsoft(Pty) Ltd16-Feb-10 19:14
professionalVimalsoft(Pty) Ltd16-Feb-10 19:14 
Thank you for your Reply. Last night i slept over it and i came back with this


/****** Object:  StoredProcedure [dbo].[sp_Traverse_Tree_Special]    Script Date: 02/16/2010 22:54:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 


ALTER  PROCEDURE [dbo].[sp_Traverse_Tree_Special] @curr int
with recompile
AS
set nocount on
if exists (select * from [tempdb].[dbo].sysobjects where id = object_id(N'[tempdb].[dbo].[#Nodes]'))
  drop table [#Nodes]
  if exists (select * from [tempdb].[dbo].sysobjects where id = object_id(N'[tempdb].[dbo].[#Nodes_FINAL]'))
  drop table [#Nodes_FINAL]

  if exists (select * from [tempdb].[dbo].sysobjects where id = object_id(N'[tempdb].[dbo].[#SemiFinal]'))
  drop table [#SemiFinal]
  
  
  if exists (select * from [tempdb].[dbo].sysobjects where id = object_id(N'[tempdb].[dbo].[#Surt1]'))
  drop table [#Surt1]
  
    if exists (select * from [tempdb].[dbo].sysobjects where id = object_id(N'[tempdb].[dbo].[#Children]'))
  drop table [#Children]
  
  
Create table [#Nodes]
(id int IDENTITY(0,1),
 NodeID int,
 parent int,
 [Description] varchar(128),
 refParent int
)
 
Create table [#Nodes_FINAL]
(id int IDENTITY(0,1),
 id2 int ,
 NodeID int,
 parent int,
 [Description] varchar(128),
 refParent int
)
INSERT INTO #Nodes
   (NodeID, Parent, [Description])
select distinct n.ID NodeID, n.Parent, nTyp.Descr [Description]
from tbl_node n
inner join tbl_node_type nTyp on nTyp.ID = n.Type and nTyp.Descr in ('Compulsory','One of')
where n.curr =  @curr
union
select distinct n.ID NodeID, n.Parent, m.Descr [Description] --, n.Type, n.Curr, m.Descr
from tbl_node n
inner join tbl_node_type nTyp on nTyp.ID = n.Type and nTyp.Descr = 'Subject'
inner join tbl_modl m on m.id = n.modl
where n.curr =  @curr
 
 select distinct  nP.id as [id], nP.NodeID, nP.parent, nP.Description, nRef.ID refParent 
into #nodes2 from #Nodes nP
left outer join #Nodes nRef on nP.Parent = nRef.NodeID	-- look up the reference id of the parent
order by  refParent,nP.Description

WITH CTENodes AS
(
SELECT
     ID,
     NODEID,
     PARENT,
     DESCRIPTION,
     REFPARENT,
     CAST(ROW_NUMBER() OVER(ORDER BY id  ) AS VARCHAR(MAX)) NodePath
  FROM #nodes2
  WHERE REFPARENT is null
UNION ALL
  SELECT
     c.ID,
     c.NODEID,
     c.PARENT,
     c.DESCRIPTION,
     c.REFPARENT,
     NodePath + '.' + CAST(ROW_NUMBER() OVER(ORDER BY C.id ) AS VARCHAR(MAX)) NodePath
  FROM CTENodes AS P
    JOIN #nodes2 AS C
      ON C.REFPARENT = P.id
)
SELECT  ID,NODEID, PARENT, DESCRIPTION, REFPARENT
into #SemiFinal
FROM CTENodes
ORDER BY NodePath,ID,REFPARENT 

--These are all Records that inherit from the Root 
select distinct s1.ID,S1.nodeid,s1.parent,s1.description,s1.RefParent into #Surt1 from #SemiFinal s1
inner join #SemiFinal s2
on s1.id = s2.id 
where s1.RefParent  = 0
and s1.id != s1.Refparent
and s1.Parent  in  (select Nodeid from #semifinal where Parent is null and ID = 0)

--Find Records that are Children
select s1.ID,S1.nodeid,s1.parent,s1.description,s1.RefParent into #Children from  #SemiFinal s1
where Refparent <> 0

--Get the Record that does not have Children
--select s1.ID,S1.nodeid,s1.parent,s1.description,s1.RefParent  from #Surt1 s1 
--where s1.id  not in (select refparent from #Children)

--delete those that have Children from the #surf1 table 
delete #Surt1
from #Surt1 s1 
where s1.id    in (select refparent from #Children)


--Then Union the Results the way you want them 
--First Union the Root
insert into #Nodes_FINAL(id2,nodeid,parent,Description,refParent)
select ID , nodeid , parent, [description], RefParent from #SemiFinal 
where Refparent is null 
 
--Follow with the Children 
insert into #Nodes_FINAL(id2,nodeid,parent,Description,refParent)
select ID  , nodeid  , parent, [description], RefParent from #Surt1
 
--Follow with the Normal
insert into #Nodes_FINAL(id2,nodeid,parent,Description,refParent)
select ID, nodeid , parent, [description], RefParent from #SemiFinal
where Refparent is not null 
and ID not in (select ID from #Surt1)



select distinct ID, nodeid , parent, [description] from #Nodes_FINAL
order by nP.id , nodeid


and it did the Trick

Thanks
Vuyiswa Maseko,

Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.

C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/

QuestionHow Pin
JustWorking16-Feb-10 1:57
JustWorking16-Feb-10 1:57 
AnswerRe: How Pin
Jörgen Andersson16-Feb-10 2:21
professionalJörgen Andersson16-Feb-10 2:21 
GeneralRe: How Pin
JustWorking16-Feb-10 19:49
JustWorking16-Feb-10 19:49 
GeneralRe: How Pin
Jörgen Andersson16-Feb-10 21:18
professionalJörgen Andersson16-Feb-10 21:18 
QuestionNot sure if this should be in Linq or Database so forgive me. How would I do a left join in linq to sql in c# Pin
tonyonlinux15-Feb-10 9:12
tonyonlinux15-Feb-10 9:12 
AnswerRe: Not sure if this should be in Linq or Database so forgive me. How would I do a left join in linq to sql in c# Pin
Pranay Rana15-Feb-10 19:22
professionalPranay Rana15-Feb-10 19:22 
Questionsql server express installation Pin
Yulianto.14-Feb-10 22:38
Yulianto.14-Feb-10 22:38 
AnswerRe: sql server express installation Pin
Not Active15-Feb-10 1:38
mentorNot Active15-Feb-10 1:38 
GeneralRe: sql server express installation Pin
εїзεїзεїз16-Feb-10 1:41
εїзεїзεїз16-Feb-10 1:41 
GeneralRe: sql server express installation Pin
Not Active16-Feb-10 6:32
mentorNot Active16-Feb-10 6:32 
GeneralRe: sql server express installation Pin
εїзεїзεїз16-Feb-10 6:58
εїзεїзεїз16-Feb-10 6:58 
GeneralRe: sql server express installation Pin
Not Active16-Feb-10 7:10
mentorNot Active16-Feb-10 7:10 
AnswerRe: sql server express installation Pin
εїзεїзεїз16-Feb-10 1:40
εїзεїзεїз16-Feb-10 1:40 
Questionexercize database schema: need advice Pin
mbrownphysics14-Feb-10 14:04
mbrownphysics14-Feb-10 14:04 
QuestionOrderBy Dilema Pin
danyDude14-Feb-10 11:53
danyDude14-Feb-10 11:53 
AnswerRe: OrderBy Dilema Pin
loyal ginger14-Feb-10 14:43
loyal ginger14-Feb-10 14:43 
GeneralRe: OrderBy Dilema Pin
danyDude14-Feb-10 15:46
danyDude14-Feb-10 15:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.