来自两个不同数据库的最新记录

时间:2017-09-07 21:14:21

标签: sql-server database tsql

感谢以前的评论指出了一个时间错误,这个问题的例子已经更新了,但问题还没有解决

我有两个数据库,让我们称之为db1和db2。每个数据库都有一个包含患者姓名的表格和一张包含患者测试记录的表格。我试图检索两个数据库之间的最新测试记录。两个数据库之间唯一的共同因素,以及我作为唯一标识符使用的是第一个Initial,Last name和DOB。

原样的代码将根据最长测试日期给出唯一的记录数。我的问题是,如果我在test_ID或test_results中发表评论,那么我将收到重复项。我尝试再次加入表并尝试CASE语句,但是对于每个记录,它将提取多个test_id(仅返回几个重复项)或者如果我在Case_results的case语句中注释(返回大约两倍的记录)。我想避免加入group1。我正在使用TSQL / Microsoft SQL analyzer 2000

我的查询看起来像这样

SELECT DISTINCT group1.LastName
                , group1.firstInitial
                , group1.DOB
                , MAX(group1.Test_date) AS Max_test_date
                -- , CASE WHEN g.Test_id IS NULL then h.Test_id 
                --        else g.Test_id 
                --        end
                -- , CASE WHEN g.Test_result IS NULL then h.Test_result
                --        else g.Test_result
                --        end
FROM(
   SELECT DISTINCT a.lastname AS LastName
                , left(a.firstname, 1) AS 'firstInitial'
                , a.dob
                , b.resultsdb1 AS Test_result
                , b.test_datedb1 AS Test_date
                , b.Lab_iddb1 AS test_id
   FROM db1.patient a
   JOIN db1.testrec b ON b.patient_ID = a.patient_ID
                         AND b.test_date = (SELECT MAX(b1.test_date)
                                             FROM db1.testrec b1
                                             WHERE b1.patient_ID = b.patient_ID
                         AND b.test_id = (SELECT MAX(b1.test_id)
                                             FROM db1.testrec b1
                                             WHERE b1.patient_ID = b.patient_ID)
   UNION
   SELECT DISTINCT c.lastname AS LastName
                , left(c.firstname, 1) AS 'firstInitial'
                , c.dob
                , d.resultsdb2 AS Test_result
                , d.test_datedb2 AS Test_date
                , d.Lab_iddb2 AS test_id
   FROM db2.patient c
   JOIN db2.testrec d ON d.patient_ID = c.patient_ID
                         AND d.test_date = (SELECT MAX(d1.test_date)
                                             FROM db2.testrec d1
                                             WHERE d1.patient_ID = d.patient_ID
                         AND d.test_id = (SELECT MAX(d1.test_id)
                                             FROM db2.testrec d1
                                             WHERE d1.patient_ID = d.patient_ID)
) as group1

LEFT JOIN db1.patient e ON e.Last_name = group1.lastname
                             AND left(e.firstname, 1) = group1.firstInitial
                             and e.dob = group1.dob
LEFT JOIN db2.patient f ON f.Last_name = group1.lastname
                             AND left(f.firstname, 1) = group1.firstInitial
                             and f.dob = group1.dob
LEFT JOIN db1.testrec g ON g.patient_ID = e.patient_ID
LEFT JOIN db1.testrec h ON h.patient_ID = f.patient_ID
GROUP BY group1.LastName
         , group1.firstInitial
         , group1.DOB

1 个答案:

答案 0 :(得分:0)

  1. 对小组的区别是必要的
  2. 合并以检查null比案例更清晰。
  3. 你错过了小组中的案例(或者你没有向他们展示),这就是问题所在。
  4. 这假设查询无效。似乎有一种更简洁的方法可以使用行号和分区来编写所有这些子查询。
  5. SELECT group1.LastName
         , group1.firstInitial
         , group1.DOB
         , MAX(group1.Test_date) AS Max_test_date
         , coalesce(g.test_ID, h.test_ID)
         , coalesce(g.test_Result, H.test_Result) 
         -- , CASE WHEN g.Test_id IS NULL then h.Test_id 
         --        else g.Test_id 
         --        end
         -- , CASE WHEN g.Test_result IS NULL then h.Test_result
         --        else g.Test_result
         --        end
    FROM(SELECT DISTINCT a.lastname AS LastName
              , left(a.firstname, 1) AS 'firstInitial'
              , a.dob
              , b.resultsdb1 AS Test_result
              , b.test_datedb1 AS Test_date
              , b.Lab_iddb1 AS test_id
         FROM db1.patient a
         JOIN db1.testrec b 
           ON b.patient_ID = a.patient_ID
          AND b.test_date = (SELECT MAX(b1.test_date)
                             FROM db1.testrec b1
                             WHERE b1.patient_ID = b.patient_ID
                               AND b.test_id = (SELECT MAX(b1.test_id)
                                              FROM db1.testrec b1
                                              WHERE b1.patient_ID = b.patient_ID)
         UNION
         SELECT DISTINCT c.lastname AS LastName
              , left(c.firstname, 1) AS 'firstInitial'
              , c.dob
              , d.resultsdb2 AS Test_result
              , d.test_datedb2 AS Test_date
              , d.Lab_iddb2 AS test_id
         FROM db2.patient c
         JOIN db2.testrec d 
           ON d.patient_ID = c.patient_ID
          AND d.test_date = (SELECT MAX(d1.test_date)
                             FROM db2.testrec d1
                             WHERE d1.patient_ID = d.patient_ID
                               AND d.test_id = (SELECT MAX(d1.test_id)
                                                FROM db2.testrec d1
                                                WHERE d1.patient_ID = d.patient_ID)) as group1
    LEFT JOIN db1.patient e 
      ON e.Last_name = group1.lastname
     AND left(e.firstname, 1) = group1.firstInitial
     AND e.dob = group1.dob
    LEFT JOIN db2.patient f 
      ON f.Last_name = group1.lastname
     AND left(f.firstname, 1) = group1.firstInitial
     AND f.dob = group1.dob
    LEFT JOIN db1.testrec g 
      ON g.patient_ID = e.patient_ID
    LEFT JOIN db1.testrec h 
      ON h.patient_ID = f.patient_ID
    GROUP BY group1.LastName
           , group1.firstInitial
           , group1.DOB
           , coalesce(g.test_ID, h.test_ID)
           , coalesce(g.test_Result, H.test_Result) 
        -- Or comment out the 2 lines above this and un-comment out the below (be sure to change the select too)
        -- , CASE WHEN g.Test_id IS NULL then h.Test_id 
        --        else g.Test_id 
        --        end
        -- , CASE WHEN g.Test_result IS NULL then h.Test_result
        --        else g.Test_result
        --        end