SQL – First medical diagnosis
I need to write a code where I need to identify the first time someone was diagnosed with a particular clinical condition. Is this the most efficient script using Min Event Dates, Distinct IDs and using the DENSE RANK functions.
with table1 as
(
SELECT
distinct a.patid,
b.person_id,
MIN(a.event_date) AS earliest_eventdate,
DENSE_RANK() OVER (PARTITION BY coalesce(b.person_id, a.patid) ORDER BY a.event_date) AS row_num
FROM