Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query Conditional Select based on Newer Entry
    primarykey
    data
    text
    <p>I have a query which searches a database of tests for failed tests and returns a certain code if there is a failure for a specific set of tests. What I need for it to do, is to only return that code if there is not a newer version of that test saved in the database that has passed. This is part of a set of queries that, given a certain project number, query every subset of that number (there are 5 test subsets), and then check and give it one of 4 different statuses. Here is an excerpt:</p> <pre><code>SELECT MAX( CASE WHEN PROJECT.PARENT_ID IN (SELECT parent_id FROM object WHERE object.id IN (SELECT parent_id FROM object WHERE object.id IN (SELECT parent_id FROM TESTRESULTS WHERE TESTRESULTS.RESULTID = '962'))) AND PROJECT.PROJECTNAME LIKE '%EMC%' THEN '4' ELSE..... </code></pre> <p>I need to check the same table for newer entries with the same name, that belong to the same project, that have passed their tests instead of failed, ie RESULTID = '961', and instead let it fall through to the other cases, rather than assign it a '4'. The dates of the tests are stored in the table. I've played with this a while on my own, doing some joins from the table back onto itself, but this parent's parent's parent's hierarchy is making it difficult to say the least. Also, I did not design this database and don't have the ability to alter the schema in any way.</p> <p>Edit: This is the relevant structure of the table, and some example records:</p> <pre><code>ID | PARENT_ID | TEST TYPE | RESULTID | TESTDATE ------------------------------------------------- 4 | 1 | ESD | 962 | 01-01-11 5 | 2 | CI | 961 | 01-02-11 6 | 3 | ESD | 961 | 02-01-11 7 | 8 | RI | 962 | 02-02-11 </code></pre> <p>I need a query that will return tests that fail (resultid 962) unless there is another test with the same name, and the same parent_id relationship described in the query above, but that passed the test (resultid 961). So, if I ran the query on 01-01-11, I would get the row with ID of 4, but if I ran it on 02-01-11, it would return no rows, and on 02-02-11 it would return the row with ID of 7. Also, there are resultids > 962 for anomalies, etc. and a resultid &lt; 961 for tests that haven't taken place yet.</p> <p>Edit again: Object Table structure</p> <pre><code>ID | PARENT_ID | Description | -------------------------------- 50 | 0 | Product Level | (Very Top) 51 | 50 | Project Level | (The PROJECT table describes this lvl) 52 | 51 | Work Request | 1 | 52 | Work Order | 2 | 52 | Work Order | 3 | 52 | Work Order | 8 | 52 | Work Order | </code></pre> <p>So, referencing the table above, each test in the TESTRESULT table is a child of a worker, which is in turn the child of a work request, which is the child of a project, which is the child of a product. I need to be able to tie every individual test result back to the project level, which is why I am running everything up to the PROJECT table, rather than the work request table.</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.
 

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