Suppress Nonadjacent Duplicates in Report

 ̄綄美尐妖づ 提交于 2019-12-23 12:58:29

问题


Medical records in my Crystal Report are sorted in this order:

...
Group 1: Score [Level of Risk]
  Group 2: Patient Name
...

Because patients are sorted by Score before Name, the report pulls in multiple entries per patient with varying scores - and since duplicate entries are not always adjacent, I can't use Previous or Next to suppress them. To fix this, I'd like to only display the latest entry for each patient based on the Assessment Date field - while maintaining the above order.

I'm convinced this behavior can be implemented with a custom SQL command to only pull in the latest entry per patient, but have had no success creating that behavior myself. How can I accomplish this compound sort?


Current SQL Statement in use:

SELECT "EpisodeSummary"."PatientID",
"EpisodeSummary"."Patient_Name",
"EpisodeSummary"."Program_Value"
"RiskRating"."Rating_Period",
"RiskRating"."Assessment_Date",
"RiskRating"."Episode_Number",
"RiskRating"."PatientID",
"Facility"."Provider_Name",

FROM (
  "SYSTEM"."EpisodeSummary"
  "EpisodeSummary"
  LEFT OUTER JOIN "FOOBARSYSTEM"."RiskAssessment" "RiskRating"
  ON (
    ("EpisodeSummary"."Episode_Number"="RiskRating"."Episode_Number")
    AND
    ("EpisodeSummary"."FacilityID"="RiskRating"."FacilityID")
  )
  AND
  ("EpisodeSummary"."PatientID"="RiskRating"."PatientID")
), "SYSTEM"."Facility" "Facility"

WHERE (
  "EpisodeSummary"."FacilityID"="Facility"."FacilityID"
)
AND "RiskRating"."PatientID" IS NOT NULL 

ORDER BY "EpisodeSummary"."Program_Value"

回答1:


The SQL code below may not be exactly correct, depending on the structure of your tables. The code below assumes the 'duplicate risk scores' were coming from the RiskAssessment table. If this is not correct, the code may need to be altered.

Essentially, we create a derived table and create a row_number for each record, based on the patientID and ordered by the assessment date - The most recent date will have the lowest number (1). Then, on the join, we restrict the resultset to only select record #1 (each patient has its own rank #1).

If this doesn't work, let me know and provide some table details -- Should the Facility table be the starting point? are there multiple entries in EpisodeSummary per patient? thanks!

SELECT es.PatientID
    ,es.Patient_Name
    ,es.Program_Value
    ,rrd.Rating_Period
    ,rrd.Assessment_Date
    ,rrd.Episode_Number
    ,rrd.PatientID
    ,f.Provider_Name
FROM SYSTEM.EpisodeSummary es
LEFT JOIN (
    --Derived Table retreiving highest risk score for each patient)
    SELECT PatientID
        ,Assessment_Date
        ,Episode_Number
        ,FacilityID
        ,Rating_Period
        ,ROW_NUMBER() OVER (
            PARTITION BY PatientID ORDER BY Assessment_Date DESC
            ) AS RN -- This code generates a row number for each record. The count is restarted for every patientID and the count starts at the most recent date.
    FROM RiskAssessment
    ) rrd
    ON es.patientID = rrd.patientid
        AND es.episode_number = rrd.episode_number
        AND es.facilityid = rrd.facilityid
        AND rrd.RN = 1 --This only retrieves one record per patient (the most recent date) from the riskassessment table 
INNER JOIN SYSTEM.Facility f
    ON es.facilityid = f.facilityid
WHERE rrd.PatientID IS NOT NULL
ORDER BY es.Program_Value


来源:https://stackoverflow.com/questions/34277232/suppress-nonadjacent-duplicates-in-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!