Hi all,
I have a group of tables containing containing relating to a maternity episode. Each episode (INCIDENT table) has a number of questions and answers associated with it (MAT_QUESTIONS and MAT_ANSWERS), which are identified using ID fields for each.
I want to extract all those patients who, prior to this episode did not have a caesarian section (question id=72637 and answer id one of 1601167,1601168,1601169,1601170,1601171,1601172), and from those 120 records, extract those that have a natural birth for this current episode (question id 72467, and answer id 1600170...etc)...(there should be 24 of those).
I want to use EXISTS in the SQL. When I run the SQL below however, it brings back the results of the outside query, and does not look at the inside query. The total figure should be ~9 patients, but it's giving me back 120 records. I'd really appreciate help!
Thanks, Fiona
select I.INCIDENTID,P.PAS_NO, P.SURNAME, P.FIRNAME, D.BIRTH_DATE,Q.DESCRIP, QS.QUESTIONTEXT,A.ID,
A.ANSWER
from MAT_INCIDENTS I, MAT_DELIVERY D, PASMAIN P,
MAT_QUESTIONNAIRE Q, MAT_QUESTIONNAIREINCIDENTS QI, MAT_QUESTIONS QS,
MAT_ANSWERS A, MAT_INCIDENTANSWERS IA
WHERE I.INCIDENTID=D.INCIDENTID
AND I.INCIDENTID=QI.INCIDENTID
AND D.INCIDENTID=IA.INCIDENTID
AND D.MOTHER_PAS_NO=P.PAS_NO
AND Q.ID=QI.QUESTIONNAIREID
AND QI.QUESTIONNAIREID=QS.QUESTIONNAIREID
AND QS.ID=A.QUESTIONID
AND A.ID=IA.ANSWERID
AND D.BIRTH_DATE BETWEEN '2007-01-01 00:00:00' AND '2007-02-01 00:00:00'
AND Q.ID IN (221)
AND QS.ID IN (72467)
AND A.ID IN (1600170,1600171,1600172,1601155)
AND EXISTS (SELECT distinct Ib.INCIDENTID,Pb.PAS_NO, Pb.SURNAME, Pb.FIRNAME, Db.BIRTH_DATE,
Qb.DESCRIP, QSb.QUESTIONTEXT,Ab.ID,Ab.ANSWER
from MAT_INCIDENTS Ib, MAT_DELIVERY Db, PASMAIN Pb,
MAT_QUESTIONNAIRE Qb, MAT_QUESTIONNAIREINCIDENTS QIb, MAT_QUESTIONS QSb,
MAT_ANSWERS Ab, MAT_INCIDENTANSWERS IAb
WHERE Ib.INCIDENTID=Db.INCIDENTID
AND Ib.INCIDENTID=QIb.INCIDENTID
AND Db.INCIDENTID=IAb.INCIDENTID
AND Db.MOTHER_PAS_NO=Pb.PAS_NO
AND Qb.ID=QIb.QUESTIONNAIREID
AND QIb.QUESTIONNAIREID=QSb.QUESTIONNAIREID
AND QSb.ID=Ab.QUESTIONID
AND Ab.ID=IAb.ANSWERID
AND Db.BIRTH_DATE BETWEEN '2007-01-01 00:00:00' AND '2007-02-01 00:00:00'
AND Qb.ID =2
AND QSb.ID =72637
AND Ab.ID IN (1601167,1601168,1601169,1601170,1601171,1601172))
ORDER BY P.PAS_NO ASC
I have a group of tables containing containing relating to a maternity episode. Each episode (INCIDENT table) has a number of questions and answers associated with it (MAT_QUESTIONS and MAT_ANSWERS), which are identified using ID fields for each.
I want to extract all those patients who, prior to this episode did not have a caesarian section (question id=72637 and answer id one of 1601167,1601168,1601169,1601170,1601171,1601172), and from those 120 records, extract those that have a natural birth for this current episode (question id 72467, and answer id 1600170...etc)...(there should be 24 of those).
I want to use EXISTS in the SQL. When I run the SQL below however, it brings back the results of the outside query, and does not look at the inside query. The total figure should be ~9 patients, but it's giving me back 120 records. I'd really appreciate help!
Thanks, Fiona
select I.INCIDENTID,P.PAS_NO, P.SURNAME, P.FIRNAME, D.BIRTH_DATE,Q.DESCRIP, QS.QUESTIONTEXT,A.ID,
A.ANSWER
from MAT_INCIDENTS I, MAT_DELIVERY D, PASMAIN P,
MAT_QUESTIONNAIRE Q, MAT_QUESTIONNAIREINCIDENTS QI, MAT_QUESTIONS QS,
MAT_ANSWERS A, MAT_INCIDENTANSWERS IA
WHERE I.INCIDENTID=D.INCIDENTID
AND I.INCIDENTID=QI.INCIDENTID
AND D.INCIDENTID=IA.INCIDENTID
AND D.MOTHER_PAS_NO=P.PAS_NO
AND Q.ID=QI.QUESTIONNAIREID
AND QI.QUESTIONNAIREID=QS.QUESTIONNAIREID
AND QS.ID=A.QUESTIONID
AND A.ID=IA.ANSWERID
AND D.BIRTH_DATE BETWEEN '2007-01-01 00:00:00' AND '2007-02-01 00:00:00'
AND Q.ID IN (221)
AND QS.ID IN (72467)
AND A.ID IN (1600170,1600171,1600172,1601155)
AND EXISTS (SELECT distinct Ib.INCIDENTID,Pb.PAS_NO, Pb.SURNAME, Pb.FIRNAME, Db.BIRTH_DATE,
Qb.DESCRIP, QSb.QUESTIONTEXT,Ab.ID,Ab.ANSWER
from MAT_INCIDENTS Ib, MAT_DELIVERY Db, PASMAIN Pb,
MAT_QUESTIONNAIRE Qb, MAT_QUESTIONNAIREINCIDENTS QIb, MAT_QUESTIONS QSb,
MAT_ANSWERS Ab, MAT_INCIDENTANSWERS IAb
WHERE Ib.INCIDENTID=Db.INCIDENTID
AND Ib.INCIDENTID=QIb.INCIDENTID
AND Db.INCIDENTID=IAb.INCIDENTID
AND Db.MOTHER_PAS_NO=Pb.PAS_NO
AND Qb.ID=QIb.QUESTIONNAIREID
AND QIb.QUESTIONNAIREID=QSb.QUESTIONNAIREID
AND QSb.ID=Ab.QUESTIONID
AND Ab.ID=IAb.ANSWERID
AND Db.BIRTH_DATE BETWEEN '2007-01-01 00:00:00' AND '2007-02-01 00:00:00'
AND Qb.ID =2
AND QSb.ID =72637
AND Ab.ID IN (1601167,1601168,1601169,1601170,1601171,1601172))
ORDER BY P.PAS_NO ASC