Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I interpret what you're saying to mean that you essentially have 3 different "batch statuses" that roughly equate to your task statuses of Pending, Processing, and Complete.</p> <p>Therefore, for any given task status you need to know 2 things:</p> <ol> <li>Does this task status indicate that the task has been started?</li> <li>Does this task status indicate that the task has been completed?</li> </ol> <p>If you have a TaskStatus table to define those statuses, then you could add some flag columns to that table, as I've done below:</p> <pre><code>CREATE TABLE TaskStatus ( taskStatusId INT, taskStatusName VARCHAR(50), taskStarted TINYINT, taskCompleted TINYINT ); INSERT INTO TaskStatus values (1, 'Pending', 0, 0); INSERT INTO TaskStatus values (2, 'Processing', 1, 0); INSERT INTO TaskStatus values (3, 'Retrying', 1, 0); INSERT INTO TaskStatus values (4, 'Complete', 1, 1); </code></pre> <p>Now you can derive the "batch status" using logic as defined in the CASE statement of the query below:</p> <pre><code>select Task.batchId, CASE WHEN max(TaskStatus.taskStarted) = 0 AND min(TaskStatus.taskCompleted) = 0 THEN 1 WHEN max(TaskStatus.taskStarted) &gt; 0 AND min(TaskStatus.taskCompleted) = 0 THEN 2 WHEN max(TaskStatus.taskStarted) &gt; 0 AND min(TaskStatus.taskCompleted) &gt; 0 THEN 4 END as batchStatusId FROM Task INNER JOIN TaskStatus on Task.taskStatus = TaskStatus.taskStatusId group by Task.batchId </code></pre> <p><a href="http://sqlfiddle.com/#!2/a7006/2/0" rel="nofollow">sqlfiddle sample</a></p> <p>In the query above, I'm using the value 1 to indicate a "batch status" of Pending, 2 to indicate a "batch status" of Processing, and 4 to indicate a "batch status" of Complete. You could create a BatchStatus table to truly define these values, though it wouldn't strictly be necessary.</p> <p>If you need to derive other "batch statuses" based on other combinations of task statuses, then you would need additional flag fields on the TaskStatus table and more complicated logic in the CASE statement.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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