Need some help with a SQL query.
I have three tables TestOptions, CreateScript and TestResults.
Testoptions holds all data for a particular test. CreateScript holds collections of tests which are run in order, denoted by index. TestResults hold the results.
I want to display a list of tests for a particular instrument. The list will contain. TestType, TestName, limits of the test and the result. Some tests may not have been carried out yet so the there will be no entry for that test in the test results table. If there the test has not been carried out, the test details still need to be retrieved. The list will be ordered by index and grouped by TestType.
So far I’ve got:
SELECT TestType, TestName, LowerLimits, UpperLimits, ResultRecorded
FROM CreateScriptTable
INNER JOIN TestOptionsTable ON CreateScriptTable.TestName =T estOptionsTable.TestName
LEFT JOIN TestResultsTable ON CreateScriptTable.TestName = TestResultsTable.TestName
WHERE CreateScriptTable.InstrumentType= 'instType'
AND jobNo ='000'
AND SerialNo ='000'
ORDER BY [Index] ASC, TestType
SerialNo and JobNo are only in TestResults table.
This query only gives the details of the tests that have been entered in the test results table. I would like all test details and the result if the test has been completed.
The expected output is as follows:
TestType TestName Lower Upper Result
type1 test1 12 20 12
type1 test2 96 108 105
type2 test2 98 108
type3 test3 95 105
The actual output is more like
TestType TestName Lower Upper Result
type1 test1 12 20 12
type1 test2 96 108 105
Only tests with entries in the results table showing.
Possibly I am not approaching the joins correctly. I have messed around with inner, left, right and full joins but to no avail and am pretty stumped right now.
You can move your criteria for columns in the left-joined table from the
WHEREclause to theJOINclause to prevent them from virtually changing your left join to an inner join like so: