This may be somewhat hard to understand, I know it's certainly going to be hard to explain so please try to bear with me and feel free to ask any questions.
The query below works.
Code:
SELECT DISTINCT
SMP.TREATMENTID
,SMPPRD.SAMPLEPRODUCTCODE
,
(
SELECT
SPCNUM
FROM
(
SELECT DISTINCT
SMP2.TREATMENTID
,SMPPRD2.SAMPLEPRODUCTCODE
,ROWNUM AS SPCNUM
FROM
GPL.GPL_SAMPLES SMP2
,GPL.GPL_SAMPLEPRODUCTLISTS SMPPRDLST2
,GPL.GPL_SAMPLEPRODUCTS SMPPRD2
WHERE
SMPPRDLST2.SAMPLEID = SMP2.SAMPLEID
AND SMPPRDLST2.SAMPLEPRODUCTID = SMPPRD2.SAMPLEPRODUCTID
AND SMP2.TREATMENTID = 301
ORDER BY SMPPRD2.SAMPLEPRODUCTCODE
)
WHERE
SAMPLEPRODUCTCODE = SMPPRD.SAMPLEPRODUCTCODE
) AS SPCNUM
FROM
GPL.GPL_SAMPLES SMP
,GPL.GPL_SAMPLEPRODUCTLISTS SMPPRDLST
,GPL.GPL_SAMPLEPRODUCTS SMPPRD
WHERE
SMPPRDLST.SAMPLEID = SMP.SAMPLEID
AND SMPPRDLST.SAMPLEPRODUCTID = SMPPRD.SAMPLEPRODUCTID
AND SMP.TRIALID = 3163;
What I want to do is instead of saying;
AND SMP2.TREATMENTID = 301
say
AND SMP2.TREATMENTID = SMP.TREATMENTID
This doesn't work because it does not recognise SMP as an alias for a table.
You might notice that this query is nested twice. On the first nest level I say;
WHERE
SAMPLEPRODUCTCODE = SMPPRD.SAMPLEPRODUCTCODE
SMPPRD.SAMPLEPRODUCTCODE is actually coming from the query above in the same way that I want to get the SMP.TREATMENTID.
The difference between this and what I want to do is the nest level. What I want to do is on the second nest level.
It may be that what I'm trying to do simply is not possible or perhaps there is another completely different approach that might do what I am trying to do. I don't know but I'm willing to accept either answer.