I have the following Query in which there is a Subquery However I don't know how to supply the values to my parameter values from my outer Query nor am I sure I can do this The subquery pulls in 5 values A1-A5 to the dataset but I am not sure how to feed the values for @Dictid and @Pid to the subquery I can set the values manually and it runs fine but the results for A1-A5 need to be based on the Current Paientid and dictatorid of the current row. Thanks for any help.
SELECT RTRIM(u.Firstname) + ' ' + RTRIM(u.LastName) AS Clinician,x.PQRI_Dictatorid,
p.PatientFirst + ' ' + p.PatientLast + '-' + x.PQRI_PatientID AS Patient, COUNT(x.PQRI_CollectionID) AS PQRI_COUNT,x.PQRI_PatientID ,x.PQRI_Dictatorid,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data X INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID ) AND (x.PQRI_QuestionID = 1)) A1,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID =@PID) AND (x.PQRI_QuestionID = 2))A2,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 3))A3,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 4))A4,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 5))A5
FROM aspnet_Users u INNER JOIN
PQRI_Data x ON u.userid1 = x.PQRI_Dictatorid INNER JOIN
Patientinfo p ON x.PQRI_PatientID = p.PatientID
GROUP BY u.LastName, u.Firstname,x.PQRI_Dictatorid, x.PQRI_PatientID, p.PatientLast, p.PatientFirst,p.patientid
having COUNT(x.PQRI_Collectionid)=5
SELECT RTRIM(u.Firstname) + ' ' + RTRIM(u.LastName) AS Clinician,x.PQRI_Dictatorid,
p.PatientFirst + ' ' + p.PatientLast + '-' + x.PQRI_PatientID AS Patient, COUNT(x.PQRI_CollectionID) AS PQRI_COUNT,x.PQRI_PatientID ,x.PQRI_Dictatorid,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data X INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID ) AND (x.PQRI_QuestionID = 1)) A1,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID =@PID) AND (x.PQRI_QuestionID = 2))A2,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 3))A3,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 4))A4,(SELECT PQRI_Questions.PQRI_Question as A1
FROM PQRI_Data x INNER JOIN
PQRI_Questions ON x.PQRI_Answer = PQRI_Questions.PQRI_QuestionID
WHERE (x.PQRI_Dictatorid = @Dictid ) AND (x.PQRI_PatientID = @PID) AND (x.PQRI_QuestionID = 5))A5
FROM aspnet_Users u INNER JOIN
PQRI_Data x ON u.userid1 = x.PQRI_Dictatorid INNER JOIN
Patientinfo p ON x.PQRI_PatientID = p.PatientID
GROUP BY u.LastName, u.Firstname,x.PQRI_Dictatorid, x.PQRI_PatientID, p.PatientLast, p.PatientFirst,p.patientid
having COUNT(x.PQRI_Collectionid)=5