0

私は開発にかなり慣れていないので、質問がばかげている場合は申し訳ありません。多くの列を結合しましたが、1 対多の関係テーブル間の結合もあります。とにかく、私が今得ている出力は

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

患者 ID に対して一次保険と二次保険の 2 つの保険のレコードを持つテーブルがあります。患者 ID ごとに 1 行で表示する方法はありますか。

現在のクエリ...

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());
4

3 に答える 3

0

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)
于 2013-08-14T18:51:35.540 に答える
0

それぞれに異なる条件とエイリアスを使用して、同じテーブルに 2 回参加できます。

   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)
于 2013-08-14T18:52:29.490 に答える
0

プライマリを取得するために 1 回、セカンダリを取得するために 1 回、テーブルに 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'
于 2013-08-14T18:50:02.873 に答える