I'm relatively new to programming in SQL, but I've been using Crystal Reports for many years, so I'm familiar with databases and tables.
I'm trying to create a view that shows a Patient ID and the COUNT of exams they've ever had. I have a tbExams table (contains the ExamID) and tbEncounters (which contains the PatientID).
I've got another table (tbcdExamStatus) that gives the exam status (I only want Ranks between 499 and 801)
In general, my result view is correct, but I've found that when a patient has exams with more than one Rank value, I get two entries in the table.
I'm guessing my group statement is incorrect. Any help is appreciated! Here is my query:
SELECT dbo.tbEncounters.PatientID, COUNT dbo.tbExams.ExamID) AS PrirorCount, dbo.tbcdExamStatus.Rank, dbo.tbcdExamStatus.Description
FROM dbo.tbExams LEFT OUTER JOIN
dbo.tbcdExamStatus ON dbo.tbExams.Status = dbo.tbcdExamStatus.Code LEFT OUTER JOIN
dbo.tbEncounters ON dbo.tbExams.EncounterID = dbo.tbEncounters.EncounterID
GROUP BY dbo.tbEncounters.PatientID, dbo.tbcdExamStatus.Rank, dbo.tbcdExamStatus.Description
HAVING (dbo.tbcdExamStatus.Rank > 499) AND (dbo.tbcdExamStatus.Rank < 801)
ORDER BY dbo.tbEncounters.PatientID DESC
I'm trying to create a view that shows a Patient ID and the COUNT of exams they've ever had. I have a tbExams table (contains the ExamID) and tbEncounters (which contains the PatientID).
I've got another table (tbcdExamStatus) that gives the exam status (I only want Ranks between 499 and 801)
In general, my result view is correct, but I've found that when a patient has exams with more than one Rank value, I get two entries in the table.
I'm guessing my group statement is incorrect. Any help is appreciated! Here is my query:
SELECT dbo.tbEncounters.PatientID, COUNT dbo.tbExams.ExamID) AS PrirorCount, dbo.tbcdExamStatus.Rank, dbo.tbcdExamStatus.Description
FROM dbo.tbExams LEFT OUTER JOIN
dbo.tbcdExamStatus ON dbo.tbExams.Status = dbo.tbcdExamStatus.Code LEFT OUTER JOIN
dbo.tbEncounters ON dbo.tbExams.EncounterID = dbo.tbEncounters.EncounterID
GROUP BY dbo.tbEncounters.PatientID, dbo.tbcdExamStatus.Rank, dbo.tbcdExamStatus.Description
HAVING (dbo.tbcdExamStatus.Rank > 499) AND (dbo.tbcdExamStatus.Rank < 801)
ORDER BY dbo.tbEncounters.PatientID DESC