Solved

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

Posted on 2016-11-30
4
41 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
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 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 24

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 24

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to use SSMS intellisense when querying 5 61
Getting certain data from a string 1 34
SQL Query to display duplicates ? 6 38
Remove Time Stamp from a Date field 4 41
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

19 Experts available now in Live!

Get 1:1 Help Now