忽略交叉表中的已抑制记录

时间:2016-10-17 19:48:26

标签: sql crystal-reports crosstab

我的报告仅根据date_of_diagnosis显示每位患者的最新诊断 - 所有其他记录均被删除:

Records after suppression

我通过交叉表中的诊断和年龄组进行总结。交叉表在打印之前进行评估,因此在交叉表填充后会发生任何抑制,共享变量或汇总的尝试。这意味着每个年龄段的总数是正确的,因为每个患者只有一个年龄 - 但如果患者有多个诊断,即使他们被抑制,他们也会多次计数:

Crosstab (Much larger in real life. Simplified/cropped for screenshot)

由于涉及大量诊断和年龄组,我绝对 必须 使用交叉表。如何让交叉表忽略被抑制的记录?或者,如果我需要使用自定义SQL命令表,如何重写现有SQL以忽略过时的记录?

Crystal自动生成的SQL(通过ODBC):

SELECT "Codes"."diagnosis_code",
       "Codes"."diagnosis_value",
       "Codes"."PATID",
       "Codes"."FACILITY",
       "Codes"."EPISODE_NUMBER",
       "Record"."date_of_diagnosis"

FROM   "SYSTEM"."Codes" "Codes",
       "SYSTEM"."Entry" "Entry",
       "SYSTEM"."Record" "Record"

WHERE  "Codes"."DiagnosisEntry"="Entry"."ID" AND
       "Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
       "Codes"."FACILITY"="Entry"."FACILITY" AND
       "Codes"."PATID"="Entry"."PATID" AND
       "Entry"."DiagnosisRecord"="Record"."ID" AND
       "Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
       "Entry"."FACILITY"="Record"."FACILITY" AND
       "Entry"."PATID"="Record"."PATID"

2 个答案:

答案 0 :(得分:1)

您只需要在一组诊断中进行最新诊断。所以我建议:

SELECT "Codes"."PATID",
   "Codes"."diagnosis_code",
   "Codes"."diagnosis_value",
   "Codes"."FACILITY",
   "Codes"."EPISODE_NUMBER",
   "Record"."date_of_diagnosis"

FROM   "SYSTEM"."Codes" "Codes",
   "SYSTEM"."Entry" "Entry",
   "SYSTEM"."Record" "Record"

WHERE  "Codes"."DiagnosisEntry"="Entry"."ID" AND
   "Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
   "Codes"."FACILITY"="Entry"."FACILITY" AND
   "Codes"."PATID"="Entry"."PATID" AND
   "Entry"."DiagnosisRecord"="Record"."ID" AND
   "Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
   "Entry"."FACILITY"="Record"."FACILITY" AND
   "Entry"."PATID"="Record"."PATID"
   AND "Entry"."date_of_diagnosis" = (SELECT MAX("date_of_diagnosis") FROM      
   "DiagonsisRecord" "A" WHERE "A"."DiagnosisRecord"="Entry"."DiagnosisRecord"  )

这应该为每位患者获得最大Date_of_Diagnosis并通过过滤器参数以获得该患者的最后诊断。

答案 1 :(得分:0)

建立Muffaddal Shakir的答案,我能够编写此查询以执行正确的过滤器:

SELECT "Codes"."PATID",
       "Codes"."diagnosis_code",
       "Codes"."diagnosis_value",
       "Codes"."FACILITY",
       "Codes"."EPISODE_NUMBER",
       "Record"."date_of_diagnosis"

FROM "SYSTEM"."codes" "Codes",
     "SYSTEM"."entry" "Entry",
     "SYSTEM"."record" "Record"

WHERE "Codes"."DiagnosisEntry"="Entry"."ID" AND
      "Codes"."EPISODE_NUMBER"="Entry"."EPISODE_NUMBER" AND
      "Codes"."FACILITY"="Entry"."FACILITY" AND
      "Codes"."PATID"="Entry"."PATID" AND
      "Entry"."DiagnosisRecord"="Record"."ID" AND
      "Entry"."EPISODE_NUMBER"="Record"."EPISODE_NUMBER" AND
      "Entry"."FACILITY"="Record"."FACILITY" AND
      "Entry"."PATID"="Record"."PATID"

AND "Record"."date_of_diagnosis" = (   
    SELECT MAX("Record2"."date_of_diagnosis")

    FROM "SYSTEM"."entry" "Entry2",
         "SYSTEM"."record" "Record2"

    WHERE "Entry2"."DiagnosisRecord"="Record2"."ID" AND
          "Entry2"."EPISODE_NUMBER"="Record2"."EPISODE_NUMBER" AND
          "Entry2"."FACILITY"="Record2"."FACILITY" AND
          "Entry2"."PATID"="Record2"."PATID" AND
          "Record"."PATID"="Record2"."PATID"
)

主要区别在于:

  1. 子查询使用主查询中的唯一别名。
  2. 最后一行"Record"."PATID"="Record2"."PATID" - 如果没有这个,查询只会回退一个诊断(整个系统中的最新诊断)。但现在它会检查最新诊断每人