2012-07-21 54 views
-2

修订那句话:另一项复杂的SQL查询

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的SAME,并且考试类型与成像一样。另外需要注意的是,MCGID是唯一的,PatientID仅对MCGID唯一。这意味着PatientID可以重新用于其他MCGID。为了运行查询,我将为它提供PatientID和MCGID。因此,我的目标是创建一个未列在TypeofExam和Indication下的invoicefields中的潜在成像和指示清单。另外,如果void = 1,它也应该忽略发票号码。

编辑:

我了解基本的SQL函数,我知道如何从多个表中提取数据,我只是不明白如何从多个表交叉引用数据。问题是我必须交叉参考大约3次的数据。所以这就是我现在所处的位置。

SELECT medicalimaging.Imaging,medicalimaging.Indication FROM medicalimaging WHERE MCGID='1032' 

我只是不确定如何告诉它从其他表中获取数据并将其与我将要提取的数据进行比较。

+2

我们可能能够帮助您,但您需要向我们展示您迄今的工作。你试过了什么,你卡在哪里? – 2012-07-21 20:36:21

+0

你可以请你的表格数据作为文本而不是图像吗? - 突出显示然后Ctl-k将其格式化为代码块。 – 2012-07-21 20:36:24

+0

猜猜我在这一个单独。 – 2012-07-21 21:29:49

回答

1

最简单的解决方法是使用存在以找到具有发票影像数据并排除他们:

select * 
from MedicalImaging mi 
where mi.mcgid = 1032 
    and not exists (select null 
        from Invoice i 
        inner join InvoiceFields f 
         on i.invoicenumber = f.invoicenumber 
        where i.studyid = mi.mcgid 
         and i.void = 0 
         and f.typeOfExam = mi.imaging 
         and f.Indication = mi.indication 
         and f.PatientID = 'PT30') 

我不得不创意在这里。我已添加InvoiceFields.Indication = MedicalImaging.Indication以缩小重复考试的范围。我不清楚这种模式背后的原因。也许你有没有公开的关于病人的表格可以解释你如何知道哪个PatientID为这个查询提供工作。

Here is DEMO @ Sql Fiddle。您可能会改变架构以提供更多信息,并发回链接。

+0

+1非常简洁(不像我的尝试!) – 2012-07-22 00:04:08

+0

无论如何,我会测试这个,出于好奇心,并评估它是否工作。谢谢!^^ – 2012-07-22 00:55:20

1

这感觉马虎,但它似乎工作。 SQLFiddle here

set @Patient = 'PT25'; 
set @MCGID = 1032; 
select distinct 
    @Patient PatientID, 
    m.id, 
    m.MCGID, 
    m.Imaging, 
    m.Indication 
from 
    MedicalImaging m 
    inner join 
    (
    select 
     i.StudyID, 
     f.PatientID, 
     f.TypeOfExam, 
     f.Indication 
    from 
     invoice i 
     inner join InvoiceFields f 
     on i.InvoiceNumber = f.InvoiceNumber 
    where 
     f.PatientID = @Patient 
     and i.StudyID = @MCGID 
) p 
    on m.MCGID = p.StudyID 
where not exists 
    (
    select 
     i.StudyID, 
     f.PatientID, 
     f.TypeOfExam, 
     f.Indication 
    from 
     invoice i 
     inner join InvoiceFields f 
     on i.InvoiceNumber = f.InvoiceNumber 
    where 
     f.PatientID = @Patient 
     and i.StudyID = @MCGID 
     and f.TypeOFExam = m.Imaging 
     and f.Indication = m.Indication 
) 
;