Revised Again:
SELECT x.Imaging, x.Indication FROM medicalimaging x
WHERE MCGID = '1036'
and x.Indication not in (
select Indication FROM invoicefields a join invoices b on a.InvoiceNumber = b.InvoiceNumber WHERE a.PatientID = '10120003' and x.MCGID = b.StudyID and x.Imaging = a.TypeOfExam
)
Order By Imaging ASC, Indication ASC
Thank you all for your answers, I did some research and this is what I cam up with.
SELECT x.Imaging, x.Indication
FROM medicalimaging x
WHERE MCGID = 'McG 1032'
AND x.Indication NOT
IN (
SELECT Indication
FROM invoicefields a
JOIN invoices b ON a.InvoiceNumber = b.InvoiceNumber
WHERE a.PatientID = '10120003'
AND x.MCGID = b.StudyID
)
I am trying to figure out what already has been invoiced and exclude it from a list. These are my tables, some sample data, and the query results.
Medical Imaging ( These are the fields to be used in the list)
id | MCGID | Imaging | Indication
1 1032 Xray Visit 1
2 1032 Xray Visit 2
3 1032 Xray Visit 3
4 1032 CT Emergency
5 1045 Xray Initial
invoice ( Generic Invoice Data)
InvoiceNumber | StudyID | TypeInvoice | void |
1 1032 Medical Imaging 0
2 1045 Medical Imaging 0
3 1032 Medical Imaging 1
4 1032 Medical Imaging 0
Invoicefields ( The Rows of charges in the Invoice )
InvoiceNumber | PatientID | TypeofExam | Indication
1 PT25 Xray Visit 1
1 PT30 Xray Visit 1
2 PT36 Xray Initial
2 PT25 Xray Initial
4 PT25 Xray Visit 2
4 PT30 Xray Visit 2
4 PT25 Xray Visit 3
After Query Results
Ex. 1 Provided MCGID=1032 and PatientID=PT25.
Results: CT , Emergency
Ex. 2 Provided MCGID=1032 and PatientID=PT30.
Results: Xray , Visit 3
CT , Emergency
So, This is the generic structure. For reference, MCGID is the SAME as StudyID, and Type of Exam is the SAME as Imaging. Another thing to note is MCGID is unique, and PatientID is only unique to the MCGID. This means that the PatientID can be reused for another MCGID. For the Query to run, I will provide it with PatientID, and MCGID. So my goal is to create a list of potential Imaging and Indications that are not listed in the invoicefields under TypeofExam and Indication. Also, it should also ignore the invoice number if void=1.
EDIT :
I understand basic sql functions, I understand how to pull data from multiple tables, I just dont get how to cross reference data from multiple tables. The issue is I have to cross reference data about 3 times. So this is where I am at now.
SELECT medicalimaging.Imaging,medicalimaging.Indication FROM medicalimaging WHERE MCGID='1032'
I am just not sure how to tell it to get the data from the other tables and compare it to the data I am about to pull.
Easiest solution is to use exist to find Imaging data having invoices and exclude them:
I had to be creative here. I’ve added
InvoiceFields.Indication = MedicalImaging.Indicationto be able to narrow down repeated exams. The reasoning behind this schema is unclear to me. Perhaps you have undisclosed tables concerning patients that would explain how you know which PatientID to supply for this query to work.Here is DEMO @ Sql Fiddle. You might alter schema there to provide more informations, and post a link back.