【问题标题】:How to join when multiple records are returned which are the same?返回多条相同的记录时如何加入?
【发布时间】:2013-08-14 18:47:42
【问题描述】:

我对开发相当陌生,如果我的问题很愚蠢,很抱歉。 我已经将许多列连接在一起,并且它在一对多关系表之间也有连接。 无论如何,我现在得到的输出是

Patient_Name    episode_id  DOB     primary_insu        sec_insur   patient_id
name,                    001    03-29-1956  MEDICAID            NULL     12
name,                    001    03-29-1956  NULL         STATEPROB     12
name2                    001    02-20-1981  AETNA             NULL      13
name2                    001    02-20-1981  NULL           MEDICAID      13

有一个表,其中有 2 条保险记录,即患者 ID 的主要保险和次要保险。有什么方法可以为每个患者 ID 在一行中显示它。

当前查询...

SELECT LTRIM(RTRIM(isnull(pat.LNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.MNAME, ''))) AS Patient_Name,
       pat.episode_id,
       CONVERT (VARCHAR (11), pat.dob, 110) AS DOB,
       CASE 
       WHEN covh.copay_priority = '1' THEN covp.payor_ID ELSE NULL 
       END AS primary_insu,
       CASE 
       WHEN covh.copay_priority = '2' THEN covp.payor_ID 
       END AS sec_insur
FROM   Patient AS pat WITH (NOLOCK)
       INNER JOIN
       coverage_history AS covh WITH (NOLOCK)
       ON pat.patient_id = covh.patient_id
          AND pat.episode_id = covh.episode_id
       INNER JOIN
       coverage AS cov WITH (NOLOCK)
       ON cov.patient_id = covh.patient_id
          AND cov.episode_id = covh.episode_id
          AND cov.hosp_status_code = covh.hosp_status_code
          AND cov.coverage_plan_id = covh.coverage_plan_id
       LEFT OUTER JOIN
       coverage_plan AS covp WITH (NOLOCK)
       ON covp.coverage_plan_id = covh.coverage_plan_id
          AND covp.hosp_status_code = covh.hosp_status_code
WHERE  covh.hosp_status_code = 'op'
       AND (covh.effective_to IS NULL
            OR covh.effective_to > GETDATE());

【问题讨论】:

  • 如果您向我们提供查询,我们可能会提供帮助

标签: sql join


【解决方案1】:

您可以使用MAX() 组合行:

SELECT LTRIM(RTRIM(isnull(pat.LNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.MNAME, ''))) AS Patient_Name,
       pat.episode_id,
       CONVERT (VARCHAR (11), pat.dob, 110) AS DOB,
       MAX(CASE 
       WHEN covh.copay_priority = '1' THEN covp.payor_ID ELSE NULL 
       END) AS primary_insu,
       MAX(CASE 
       WHEN covh.copay_priority = '2' THEN covp.payor_ID 
       END) AS sec_insur
FROM   Patient AS pat WITH (NOLOCK)
       INNER JOIN
       coverage_history AS covh WITH (NOLOCK)
       ON pat.patient_id = covh.patient_id
          AND pat.episode_id = covh.episode_id
       INNER JOIN
       coverage AS cov WITH (NOLOCK)
       ON cov.patient_id = covh.patient_id
          AND cov.episode_id = covh.episode_id
          AND cov.hosp_status_code = covh.hosp_status_code
          AND cov.coverage_plan_id = covh.coverage_plan_id
       LEFT OUTER JOIN
       coverage_plan AS covp WITH (NOLOCK)
       ON covp.coverage_plan_id = covh.coverage_plan_id
          AND covp.hosp_status_code = covh.hosp_status_code
WHERE  covh.hosp_status_code = 'op'
       AND (covh.effective_to IS NULL
            OR covh.effective_to > GETDATE())
GROUP BY LTRIM(RTRIM(isnull(pat.LNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.MNAME, ''))),
       pat.episode_id,
       CONVERT (VARCHAR (11), pat.dob, 110)

【讨论】:

  • 没有主保和次保字段,它们有一个名为'copay_priority'的列,当它为1时,它是主要的,当它是2时,它是次要的
  • Well 2 JOINS 在这种情况下可能更有意义,但您仍然可以使用MAX(),方法是将您拥有的任何逻辑包装在MAX() 中的这两个字段中
  • 已更新以使用您的查询。
  • 我试过了,好吧,还有一个问题。有一级和二级保险的地址字段,我怎样才能把它也放入查询中?
  • 为了避免使用MAX(),你必须JOIN两次,添加地址字段你只需复制逻辑MAX(CASE WHEN covh.copay_priority = '2' THEN covp.payor_ID END) AS sec_insur并将THEN替换为相关的地址字段.
【解决方案2】:

您将加入表两次,一次获得主要,一次获得次要。使用左连接,因为并非所有患者都有保险。示例如下。您需要根据实际的表结构和获取其他信息所需的任何其他连接来调整它。

SELECT p.Patient_name,  i1.insurancename AS primary_insure, i2.insuranceName AS secondary_insure
FROM patient P
LEFT JOIN PatientInsurance i1 ON p.patient_id = i1.patient_id AND i1.insurance_type = 'Primary'
LEFT JOIN PatientInsurance i2 ON p.patient_id = i2.patient_id  AND i2.insurance_type = 'Secondary'

【讨论】:

  • 两次join时,返回的表数也会更多吧?我只想显示一条记录。
  • 这就是我在 on 子句中添加 where insurancetype = 的原因。您需要提出一个条件,使加入一对一,这是最简单的方法。
  • 知道了.. 谢谢.. 好吧.. 我尝试使用 max 和 2 joins.. 工作正常.. 非常感谢你们.. 我不知道没有我会做什么你们。
【解决方案3】:

您可以加入同一个表两次,每次使用不同的条件和别名。

   SELECT 
    LTRIM(RTRIM(isnull(pat.LNAME,''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME,''))) + ', ' + LTRIM(RTRIM(isnull(pat.MNAME,''))) AS Patient_Name, 
    pat.episode_id, 
    convert(varchar(11),pat.dob,110) AS DOB, 
    pIns.payor_ID AS Primary, 
    sIns.payor_ID AS Secondary
from 
    Patient pat with (nolock) 
    left join coverage_history pIns with (nolock) on 
        (pat.patient_id=pIns.patient_id)
        and 
        (pat.episode_id=pIns.episode_id)
        and 
        (pIns.copay_priority = '1')
    left join coverage_history sIns with (nolock) on 
        (pat.patient_id=sIns.patient_id)
        and 
        (pat.episode_id=sIns.episode_id)
        and 
        (sIns.copay_priority = '2')
 (remainder of where clause)

【讨论】:

  • 它的代码很大,所以我没有复制它,我正在编辑它并粘贴它
  • SELECT LTRIM(RTRIM(isnull(pat.LNAME,''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME,'')))+ ', '+ LTRIM (RTRIM(isnull(pat.MNAME,''))) AS Patient_Name, pat.episode_id, convert(varchar(11),pat.dob,110) AS DOB, primary_insu=case when covh.copay_priority='1' then covp .payor_ID else null end, sec_insur=case when covh.copay_priority='2' then covp.payor_ID end from Patient pat with (nolock) inner join coverage_history covh with (nolock) on pat.patient_id=covh.patient_id and pat.episode_id= covh.episode_id
  • 内连接覆盖 cov,在 cov.patient_id=covh.patient_id 和 cov.episode_id=covh.episode_id 和 cov.hosp_status_code=covh.hosp_status_code 和 cov.coverage_plan_id=covh.coverage_plan_id 上使用 (nolock) 左连接在 covp.coverage_plan_id=covh.coverage_plan_id 和 covp.hosp_status_code=covh.hosp_status_code 上带有 (nolock) 的coverage_plan covp,其中 covh.hosp_status_code='op' 和 (covh.effective_to 为 NULL 或 covh.effective_to>getdate())
  • 编辑了我的答案以匹配您的架构。显然无法测试,但您应该能够看到它的前进方向。
  • 将 OP 的查询移到问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
相关资源
最近更新 更多