从连接表中返回不同的记录

时间:2017-05-25 20:38:37

标签: sql

enter image description here我正试图弄清楚如何从下面的报告查询中返回来自病人案例表的不同记录:

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

我已尝试将联接更改为左但没有运气。请建议如何使这项工作。感谢

1 个答案:

答案 0 :(得分:0)

来自patientcase表&#34;的不同记录是什么意思?这里吗?

您的记录不是来自patientcase表,认为它们来自一种匿名的,未命名的表,它结合了您的联接和定义的记录。条件 - 那将是什么&#34; distinct&#34;在这种情况下?

如果你想在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 ... 上的 不同记录中进行联接 - 比它更简单:从/使用子查询选择/加入,以此方式缩小源表,类似的东西:

{{1}}

如果那不是你想要的 - 请详细说明,因为它是明确的。