Note that there are some explanatory texts on larger screens.

plurals
  1. POTSQL CTE and a Graph of Sorts
    primarykey
    data
    text
    <p>With a table of the following structure and sample data:</p> <pre><code>TableActivity ------------- Type VARCHAR(8) Activity VARCHAR(8) RelatedActivity VARCHAR(8) Type Activity RelatedActivity ------------------------------------------ Start a - Transfer a b Start b - Transfer b c Start c - Stop c - Transfer c b Stop b - Transfer b a Stop a - </code></pre> <p>Would it be possible to write a CTE query to accomplish the following:</p> <pre><code>GetActivities('a') Order Activities ------------------- 0 a 1 b 2 c </code></pre> <p>I'm having a tough time writing one that stops returning rows in the recursive statement. </p> <p>Any ideas?</p> <p><strong>Edit</strong></p> <p>To clarify GetActivities('a'). This function should find the 'Start' activity of 'a' and proceed to find any 'Transfer' activities on 'a'. At that the point the function can then recurse with 'b' and consequently 'c' with the sample data. The query should return all activities related to 'a' via 'Transfers'. This nesting of activities can go as deep as need be and is unknown (so no unions). The difficulty I'm having is that there is another 'Transfer' back down e.g. 'b' -> 'a'. You can see how this would create a loop in the recursive query.</p> <p>One more clarification: the transfers in the activity table behave as a stack. Here is how the data is populated in the table (in C#):</p> <pre><code> using (Activity.Start("a")) { // transfer to 'b' under covers using (Activity.Start("b")) { // transfer to 'c' under covers using (Activity.Start("c")) { } // transfer to 'b' under covers } // transfer to 'a' under covers } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload