2012-07-21 52 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 
) 
;