【问题标题】:Most Recent Record from two different databases来自两个不同数据库的最新记录
【发布时间】:2017-09-07 21:14:20
【问题描述】:

感谢先前指出序列错误的评论,此查询示例已更新,但问题尚未解决

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

按原样的代码将为我提供到最大测试日期的唯一记录数。我的问题是,如果我在 test_ID 或 test_results 中发表评论,那么我会收到重复的。我尝试再次加入表并尝试使用 CASE 语句,但是对于每条记录,它将提取多个 test_id(仅返回几个重复项),或者如果我在 Test_results 的 case 语句中进行评论(返回大约两倍的记录)。我想避免加入 group1。我正在使用 TSQL/Microsoft SQL 分析器 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

【问题讨论】:

  • 正如发布的那样,此查询将不会运行,因为您在加入之前有一个 group by。您的每组子查询也至少缺少一个括号。
  • 谢谢。我做了更正!
  • 而不是大小写使用 coalesce(g.test_ID,h.Test_ID)coalesce(g.test_result, H.Test_result) 确保两者都被添加到组中。假设其他一切正常,那么 group by 中缺少的元素是真正的问题,您可以在那里重复 case 语句;但我发现在这种情况下合并更优雅,因为你检查的所有内容都是空的。
  • 您的查询仍有语法错误。以 SELECT MAX(d1.test_date) 开头的子查询没有右括号。因此,您对代码原样返回某些内容的评论是不准确的。此处发布的代码不能与您正在运行的代码相同。

标签: sql-server database tsql


【解决方案1】:
  1. 组别需要不同的内容
  2. 合并检查 null 比 case 更简洁。
  3. 您错过了组中的案例(或者您没有向我展示)这可能是问题所在。
  4. 这假定查询以其他方式工作。似乎有一种更简洁的方法可以使用行号和分区来编写所有这些子查询。

.

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

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多