再次修订:
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
谢谢大家的回答,我做了一些研究,这就是我想出来的。
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
)
我试图弄清楚已经开具发票的内容并将其从列表中排除。这些是我的表、一些示例数据和查询结果。
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
所以,这是通用结构。作为参考,MCGID 与 StudyID 相同,而 Type of Exam 与 Imaging 相同。另外需要注意的是MCGID是唯一的,而PatientID是唯一的MCGID。这意味着 PatientID 可以重复用于另一个 MCGID。为了运行查询,我将为它提供 PatientID 和 MCGID。所以我的目标是创建一个潜在的成像和指示列表,这些列表未在 TypeofExam 和指示下的发票字段中列出。此外,如果 void=1,它也应该忽略发票编号。
编辑 :
我了解基本的 sql 函数,我了解如何从多个表中提取数据,我只是不知道如何从多个表中交叉引用数据。问题是我必须交叉引用数据大约 3 次。所以这就是我现在的位置。
SELECT medicalimaging.Imaging,medicalimaging.Indication FROM medicalimaging WHERE MCGID='1032'
我只是不确定如何告诉它从其他表中获取数据并将其与我将要提取的数据进行比较。