Solved

Recursive query to determine worfklow with dependencies

Posted on 2013-11-14
8
168 Views
Last Modified: 2014-01-15
Hello,

I have table with widgets and a table with tasks.  Each widget has one of each of the tasks, none get left out.

The tables look something like so:

tblWidget
Widget_ID INT
Deadline DATETIME

tblTask
Task_ID CHAR(5)
Duration_Days INT

tblWorkflow
Widget_ID INT
Task_ID CHAR(5)
Completion_Date_Estimate DATETIME
Completion_Date_Actual DATETIME

tblWorkflowHierarchy
Workflow_ID INT  **Identity
Task_ID CHAR(5)    **FK to tblTask
Parent_Task_ID CHAR(5)  **FK to tblTask


The widgets each have a deadline of when they need to be completed.  Several of the tasks can run in parallel, but cannot start if their parent task has not been completed.  (i.e. has no value in Completion_Date_Actual)

I need a query that can backwards calculate the "maximum path", or the maximum length of time the longest line of tasks line of tasks.  So, if we knew in advance that the longest path was 29 days, given a due date of 6/30/13, the query would return 6/29/13.

Thanks in advance,
-Torrwin
0
Comment
Question by:Torrwin
  • 4
  • 4
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 39649103
Hi Torrwin,

The key items in a recursive query are to establish the correct starting point and to sequence the items in the correct order.

One of the nice things about CTE syntax is that if we know the logical order we can create a row order for the recursive query.  

With that as a background, how do you determine the assembly order?  And how do you identify the parallel tasks that can, perhaps, be discounted?


Kent
0
 
LVL 13

Author Comment

by:Torrwin
ID: 39649558
Well, since they're in parallel we can't assign them a straight index to order them by.  I was planning to use recursively go through the hierarchy table. Like so:

Iteration 1:
Select Task_ID FROM tblWorkflowHierarchy WHERE Parent_Task_ID is Null

Iteration 2:
Loop through results from iteration 1 and do the following on each:
Select Task_ID FROM tblWorkflowHierarchy WHERE Parent_Task_ID = Iteration 1 Task_ID

...etc...
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39649743
Ah.  Then it gets easier.

Let's start with this query.  At line five, modify the commented filter rule to select only a few widget_ID values so the results are manageable.

WITH rq AS
(
  SELECT 1 as level, Widget_ID, Task_ID FROM tblWorkflowHierarchy 
  WHERE Parent_Task_ID is NULL
--   AND Widget_ID between 10 and 15

  UNION ALL

  SELECT level+1, Widget_ID, Task_ID FROM tblWorkflowHierarchy t0,  Rq
  WHERE t0.Widget_ID = Rq.Widget_ID
    AND t0.Task_ID = r1.Widget_ID + 1
)
SELECT * FROM rq;

Open in new window

0
 
LVL 13

Author Comment

by:Torrwin
ID: 39650180
Hmm, Task_ID isn't an integer so it can't ever be equal to Widget_ID + 1
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 45

Expert Comment

by:Kdo
ID: 39650184
Ok.  So how do we serialize the rows for each widget?  Does the value of Task_ID increase "AAAAA", "AAAAB", "AACGR", etc.?
0
 
LVL 13

Accepted Solution

by:
Torrwin earned 0 total points
ID: 39650196
I edited it a bit, and this seems to be working.  However, there's one little hiccup.  A few of the tasks have multi-level parents so they get listed twice.  For example, a task at level 7 cant be completed until both a parallel task at level 2 and a parallel task at level 5 have been completed.

WITH rq AS
(
  SELECT 1 as level, Task_ID FROM tblWorkflowHierarchy
  WHERE Parent_Task_ID is NULL

  UNION ALL

  SELECT level+1, t0.Task_ID  FROM tblWorkflowHierarchy t0,  Rq
  WHERE t0.Parent_Task_ID = Rq.Task_ID
		

)
SELECT * FROM rq
ORDER BY LEVEL

Open in new window

0
 
LVL 45

Expert Comment

by:Kdo
ID: 39650991
That brings us back to my very first post -- "The key items in a recursive query are to establish the correct starting point and to sequence the items in the correct order".

How do we sequence these events from the data?
0
 
LVL 13

Author Closing Comment

by:Torrwin
ID: 39781809
This set me down the right track, thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 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

20 Experts available now in Live!

Get 1:1 Help Now