Solved

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

Posted on 2016-11-30
4
18 Views
1 Endorsement
Last Modified: 2016-12-01
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
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
<<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
Comment Utility
thank you! i apologize for the pictures and no syntax.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now