Created
September 3, 2014 14:25
-
-
Save dozortsev/91a80e9333266a45fce0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
IF((SELECT | |
count(*) | |
FROM | |
PatientMedicalRecord | |
WHERE | |
PatientId = 30819 | |
AND Domain = 'encounter') >= 2 | |
AND duplicates = (SELECT | |
count(*) | |
FROM | |
PatientMedicalRecord | |
WHERE | |
PatientId = 30819 | |
AND Domain = 'encounter'), | |
NULL, | |
IF((SELECT | |
count(*) | |
FROM | |
PatientMedicalRecord | |
WHERE | |
PatientId = 30819 | |
AND Domain = 'encounter') >= 2 | |
AND BIT_AND(larger), | |
30819, | |
NULL)) AS PatientId | |
FROM | |
(SELECT | |
a.Date, | |
IFNULL(DATEDIFF((SELECT | |
pmr.Date | |
FROM | |
PatientMedicalRecord pmr | |
WHERE | |
pmr.Date > a.Date | |
AND pmr.PatientId = 30819 | |
AND pmr.Domain = "encounter" | |
ORDER BY | |
pmr.Date | |
LIMIT 1), | |
a.Date) >= 90, | |
1) AS larger, | |
(SELECT | |
COUNT(dd.Date) | |
FROM | |
PatientMedicalRecord dd | |
WHERE | |
dd.Date = a.Date | |
AND dd.PatientId = 30819 | |
AND dd.Domain = "encounter") AS duplicates | |
FROM | |
PatientMedicalRecord a | |
WHERE | |
a.PatientId = 30819 | |
AND a.Domain = "encounter" | |
) AS x, PatientMedicalRecord enc | |
WHERE | |
enc.PatientId = 30819 | |
AND enc.Domain = "encounter" | |
LIMIT 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment