Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

order a table based on column relationship (parent-child), can it be done?

Posted on 2016-11-30
4
Medium Priority
?
67 Views
1 Endorsement
Last Modified: 2016-12-28
hello everyone. i'm sure this can be done and i'm sure i can figure it out with enough time, however i'm asking all you experts out there for a quick reference.

i've got a table, 3 columns: ProcessName, Parent, Child

this table houses a process flow for a job (Parent) that starts then finishes and executes the next job (Child) then so forth. each time a job can start and execute another job, a record of the connection is represented.

this table is full of processes and parent jobs executing child jobs. to make it simple, i will use a simple table with simple data:

1.png
what i'm trying to do is have a select statement that orders the table by the process name descending, then order the jobs in which they execute. using the child to reference the parent and going backwards, i should be able to return the following result set:

2.png
notice that the process name is ordered by descending and that the first record of each process shows that a job fires off another job, then the next row, that job fires off another, and so forth until we hit NULL were the job doesn't fire off any job and stops.

i'm guessing i would have to join the table back into itself. any insights would be greatly appreciated it!
1
Comment
Question by:Doua Vang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 41908122
yes it is possible with a help of a recursive CTE:

declare @t table (ProcessName varchar(20),Parent varchar(20),Child varchar(20))
insert into @t values('ShareDrafts','Job12','Job03'),('ShareDrafts','Job13','Job58'),('ShareDrafts','Job03','Job81'),('ShareDrafts','Job58',NULL),('ShareDrafts','Job81','Job13')

;with a as (
select *, 1 as Level FROM @t where Child IS NULL
UNION ALL
SELECT b.*, a.level+1 
FROM a INNER JOIN @t b ON a.ProcessName = b.ProcessName and a.Parent = b.Child)
select * from a order by level desc

Open in new window


Result:
ProcessName          Parent               Child                Level
-------------------- -------------------- -------------------- -----------
ShareDrafts          Job12                Job03                5
ShareDrafts          Job03                Job81                4
ShareDrafts          Job81                Job13                3
ShareDrafts          Job13                Job58                2
ShareDrafts          Job58                NULL                 1

(5 row(s) affected)

Open in new window

My advise for the future: try putting the workable DDE and insert queries of your sample data (see above) to speed up the response time. Pictures don't help us much. We are all busy as you are and typing these queries very often discourage us from answering the questions
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41908444
<<Nopts..>> Added ProcessName in the order by clause.  <<Checking if it is possible with recursion>>

;with a as (
select *, 1 as Level FROM X where Child IS NULL
UNION ALL
SELECT b.*, a.level+1 
FROM a INNER JOIN X b ON a.ProcessName = b.ProcessName and a.Parent = b.Child)
select * from a order by ProcessName DESC,level desc

Open in new window

0
 

Author Closing Comment

by:Doua Vang
ID: 41909039
thank you! i apologize for the pictures and no syntax.
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41909539
Hi Doua,

I have simple solution without recursion. Please open a new question if you need it since this one is already closed.

Thank You!
0

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question