Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Recursive query to determine worfklow with dependencies

Posted on 2013-11-14
Medium Priority
182 Views
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

Duration_Days INT

tblWorkflow
Widget_ID INT
Completion_Date_Estimate DATETIME
Completion_Date_Actual DATETIME

tblWorkflowHierarchy
Workflow_ID INT  **Identity

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.

-Torrwin
0
Question by:Torrwin
• 4
• 4

LVL 46

Expert Comment

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

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:

Iteration 2:
Loop through results from iteration 1 and do the following on each:

...etc...
0

LVL 46

Expert Comment

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
--   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;
``````
0

LVL 13

Author Comment

ID: 39650180
Hmm, Task_ID isn't an integer so it can't ever be equal to Widget_ID + 1
0

LVL 46

Expert Comment

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

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

UNION ALL

SELECT level+1, t0.Task_ID  FROM tblWorkflowHierarchy t0,  Rq

)
SELECT * FROM rq
ORDER BY LEVEL
``````
0

LVL 46

Expert Comment

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

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

## Featured Post

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then readingâ€¦
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
###### Suggested Courses
Course of the Month10 days, 11 hours left to enroll