I have a query that will eliminate the duplicates and only shows the unique records.
SELECT DISTINCT TBLPATIENT.MRN
FROM tblpatient
Output:
MRN
------
15257
15283
15285
15290
15291
15302
However, I have additional columns that I need to show up but I want only the unique MRNs.
SELECT
v.PatientID,
p.firstname,
p.lastname,
p.dob,
p.mrn,
s.VisitID,
v.AdmitedDate
FROM
tblPatient p
JOIN
tblPatientVisit v ON p.id = v.PatientID
JOIN
tblPatientSmokingScreenOrder s ON v.id = s.VisitID
JOIN
DESCRIPTOR t ON s.SMOKING_STATUS_DSC_ID = DESCRIPTOR_ID
WHERE
isdate(p.DOB) = 1
AND CONVERT(date,p.DOB) <'12/10/2000'
AND v.PatientType = 'I' and
isdate(v.AdmitedDate) = 1
AND CONVERT(date,v.AdmitedDate) > '06/16/2013 16:16'
Output:
PatientID firstname lastname dob mrn VisitID AdmitedDate
---------------------------------------------------------------------------
1 JAMES TEST 6/11/1942 100241 1 54:00.0
10 TEST3 DEMOGRAPHIC 4/7/1980 100251 13 39:00.0
5 TEST2 DEMOGRAPHIC 8/31/1938 3 12 36:00.0
21 ZACHARY EFRON 11/2/1976 100267 24 11:00.0
16 PATIENT DEMO 2/28/1943 100260 26 56:00.0
17 ALICE WONDERLAND 9/20/1942 100261 20 14:00.0
23 TEST5 BROWN 5/6/1965 15285 27 40:00.0
23 Test5 BROWN 5/6/1965 15285 27 40:00.0
How do I take the script above and refactor it to only show Test5 Brown once?
tblpatientonly has one row per patient.tblpatientvisitlikely has 2+ rows for patients with 2+ visits (however it's unclear whether it has 2+ rows for the same visit, due to other descriptive columns, could be your problem), and it is unclear also whether there might be a 1:M relationship betweentblpatientvisitandtblpatientsmokingscreenorder. Usingdistinctwould just be an attempt to bandaid the query. We need to know where the 1:M rel. istblpatienthas effective dated rows reflecting changes to a patient's name and other info over time. What fields are on that table? Perhaps you need a correlated subquery to show the max effective dated record as of the visit.