2017-05-25 142 views
0

enter image description here我試圖找出如何從patientcase表從下面的報表查詢返回不同的記錄:連接表返回不同的記錄

select distinct rp.patientid 
    p.mdpa11cd as mma, p.mdpa12cd as mmb, 
    test.mica1cd as mmc, test.mica2cd as mmdr, 
    lastnm, firstnm, hospitalid, ssnbr, 
    rp.relationshiptypecd, 
    isnull(p.ma1eqcd, '') A1s, 
    isnull(p.ma2eqcd, '') A2s, 
    isnull(p.mb1eqcd, '') B1s, 
    isnull(p.mb2eqcd, '') B2s, 
    isnull(p.mc1eqcd, '') C1s, 
    isnull(p.mc2eqcd, '') C2s, 
    isnull(p.mdrb11eqcd, '') DR1s, 
    isnull(p.mdrb12eqcd, '') DR2s, 
    isnull(p.mdqb11eqcd, '') DQ1s, 
    isnull(p.mdqb12eqcd, '') DQ2s, 
    isnull(p.mdpb11eqcd, '') DP1s, 
    isnull(p.mdpb12eqcd, '') DP2s, 
    isnull(p.mdrb31eqcd, '') DRB31s, 
    isnull(p.mdrb32eqcd, '') DRB32s, 
    isnull(p.mdrb41eqcd, '') DRB41s, 
    isnull(p.mdrb42eqcd, '') DRB42s, 
    isnull(p.mdrb51eqcd, '') DRB51s, 
    isnull(p.mdrb52eqcd, '') DRB52s, 
    p.ma1cd, p.ma2cd, p.mb1cd, p.mb2cd, p.mc1cd, p.mc2cd, 
    p.mdrb11cd, p.mdrb12cd, p.mdqb11cd, p.mdqb12cd, p.mdpb11cd, 
    p.mdpb12cd, p.mdrb31cd, p.mdrb32cd, p.mdrb41cd, p.mdrb42cd, 
    p.mdrb51cd, p.mdrb52cd, p.lastmolecularsampledt, 
    isnull(rp.mamismatchcd, '') MMa, 
    isnull(rp.mbmismatchcd, '') MMb, 
    isnull(rp.mcmismatchcd, '') MMc, 
    isnull(rp.mdrb1mismatchcd, '') MMdr, 
    isnull(rp.mdqb1mismatchcd, '') MMdq, 
    rp.mdpb1mismatchcd MMdpb1, 
    isnull(rp.mamismatchantigencd, '') Ma, 
    isnull(rp.mbmismatchantigencd, '') Mb, 
    isnull(rp.mcmismatchantigencd, '') Mc, 
    isnull(rp.mdrb1mismatchantigencd, '') Mdr, 
    isnull(rp.mdqb1mismatchantigencd, '') Mdq, 
    rp.mdpb1mismatchantigencd Mdpb1, suppressnameind, patienttypecd, 
    isnull(p.mdqa11eqcd, '') DQA1s, 
    isnull(p.mdqa12eqcd, '') DQA2s, 
    p.mdqa11cd, p.mdqa12cd, rp.mdqa1mismatchcd MMdqa1, 
    rp.mdqa1mismatchantigencd Mdqa1, p.mbw1cd, p.mbw2cd, 
    rp.haplotype1cd, rp.haplotype2cd 
from 
    patientcase 
inner join 
    relatedpatient rp on rp.caseid = patientcase.caseid 
inner join 
    patient p on rp.relatedpatientid = P.patientid 
left join 
    sample on sample.patientid = p.patientid 
left join 
    test on test.sampleid = sample.sampleid 
where 
    patientcase.caseid = `<Patient Cases>` 
    and rp.relatedpatientid IN `(<Donor>)` 
order by 
    rp.ordernbr asc, sample.sampledt desc 

我試圖改變加入到離開,但沒有運氣。請建議如何使這項工作。謝謝

+0

你有沒有試過在'SELECT'之後加入'DISTINCT'? – SQLChao

+0

是的,但它沒有奏效。 – user3781528

+1

您正在使用哪些DBMS? –

回答

0

這是什麼意思「patientcase表中的不同記錄」在這裏?

您的記錄不是來自patientcase表,認爲它們來自一種匿名,未命名的表,它結合了由您的連接條件定義的記錄 - 那麼在這種情況下什麼是「不同的」?

如果你想要做你從patientcase加入重複的記錄 - 比它更簡單:選擇/從/加入與子查詢變窄源表的方式,是這樣的:

select 
    p.mdpa11cd as mma, p.mdpa12cd as mmb, 
    test.mica1cd as mmc, test.mica2cd as mmdr, 
    pcd.lastnm, pcd.firstnm, pcd.hospitalid, pcd.ssnbr, 
    rp.relationshiptypecd, 
    ... 
from 
    (SELECT DISTINCT lastnm, firstnm, hospitalid, ssnbr, caseid, ... FROM patientcase) pcd 
inner join 
    relatedpatient rp on rp.caseid = pcd.caseid 
... 

如果是不是你想要的 - 請詳細說明,因爲它是從明確的方式。

+0

那麼,select語句會是一樣的,對嗎?您只是在「from」之後添加查詢,並且該查詢將包含來自patientcase的所有字段,對嗎?否則,其他一切都是一樣的?謝謝 – user3781528

+0

這取決於你真正想要得到什麼。對我而言,這還不清楚,我必須承認。 –

相關問題