-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL
97 lines (85 loc) · 3.25 KB
/
SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
SELECT DISTINCT
PATIENT_IEN AS ACTIVE_PT_PATIENT_IEN,
PATIENT_DOB AS ACTIVE_PT_PATIENT_DOB,
PATIENT_SEX AS ACTIVE_PT_PATIENT_SEX,
MAILING_ADDRESSZIP AS ZIP_CODE,
P.DIAGNOSIS_CODE_NUMBER AS DIAGNOSIS_CODE_NUMBER,
P.STATUS,
PATIENT_SSN AS ACTIVE_PT_PATIENT_SSN,
COMMUNITY,
TRIBE_OF_MEMBERSHIP_NAME
FROM
( SELECT
V.VISITADMIT_DATETIME,
V.IEN,
V.PATIENT_NAME AS PATIENT_IEN,
V.PATIENT_NAME -> CURRENT_COMMUNITY AS COMMUNITY,
V.PATIENT_NAME -> CURRENT_RESIDENCE_PTR -> SERVICE_UNIT_NAME AS COMMUNITY_SU,
V.PATIENT_NAME -> TRIBE_OF_MEMBERSHIP_NAME,
V.PATIENT_NAME -> TRIBE_OF_MEMBERSHIP,
V.PATIENT_NAME -> DOB AS PATIENT_DOB,
V.PATIENT_NAME -> SEX AS PATIENT_SEX,
V.PATIENT_NAME -> SSN AS PATIENT_SSN,
V.PATIENT_NAME -> MAILING_ADDRESSZIP
FROM BMW.VISIT V
INNER JOIN BMW.VA_PATIENT VAP ON V.PATIENT_NAME = VAP.IEN
WHERE
AND V.VISITADMIT_DATETIME >=3221001
AND V.DEPENDENT_ENTRY_COUNT IS NOT NULL
AND V.DELETE_FLAG IS NULL
AND V.CLINIC_NAME IS NOT NULL
AND V.CLINIC -> CODE NOT IN (11, 68, 51)
AND V.SERVICE_CATEGORY NOT IN ('DAILY HOSP DATA', 'ANCILLARY PACKAGE DAILY', 'EVENT (HISTORICAL)', 'CHART REVIEW')
AND V.PATIENT_NAME -> DATE_OF_DEATH IS NULL
AND (
(TO_NUMBER(V.PATIENT_NAME -> TRIBE_OF_MEMBERSHIP) > 1
AND TO_NUMBER(V.PATIENT_NAME -> TRIBE_OF_MEMBERSHIP) < 968)
OR (TO_NUMBER(V.PATIENT_NAME -> TRIBE_OF_MEMBERSHIP) IN (997, 999))
)
AND VAP.PATIENT_MERGED_TO IS NULL
) AS DISTINCTPatients
LEFT JOIN BMW.Problem AS P ON DISTINCTPatients.PATIENT_IEN = P.PATIENT_NAME
WHERE P.STATUS NOT IN ('DELETED')
==============
SELECT DISTINCT
DISTINCTPatients.PATIENT_IEN AS ACTIVE_PT_PATIENT_IEN,
DISTINCTPatients.PATIENT_DOB AS ACTIVE_PT_PATIENT_DOB,
DISTINCTPatients.PATIENT_SEX AS ACTIVE_PT_PATIENT_SEX,
DISTINCTPatients.MAILING_ADDRESSZIP AS ZIP_CODE,
P.DIAGNOSIS_CODE_NUMBER AS DIAGNOSIS_CODE_NUMBER,
P.STATUS,
DISTINCTPatients.PATIENT_SSN AS ACTIVE_PT_PATIENT_SSN,
DISTINCTPatients.COMMUNITY,
DISTINCTPatients.TRIBE_OF_MEMBERSHIP_NAME
FROM
(
SELECT
V.VISITADMIT_DATETIME,
V.IEN,
V.PATIENT_NAME AS PATIENT_IEN,
V.CURRENT_COMMUNITY AS COMMUNITY,
V.CURRENT_RESIDENCE_PTR -> SERVICE_UNIT_NAME AS COMMUNITY_SU,
V.TRIBE_OF_MEMBERSHIP_NAME,
V.TRIBE_OF_MEMBERSHIP,
V.DOB AS PATIENT_DOB,
V.SEX AS PATIENT_SEX,
V.SSN AS PATIENT_SSN,
V.MAILING_ADDRESSZIP
FROM BMW.VISIT V
INNER JOIN BMW.VA_PATIENT VAP ON V.PATIENT_NAME = VAP.IEN
WHERE
V.VISITADMIT_DATETIME >= 3221001
AND V.DEPENDENT_ENTRY_COUNT IS NOT NULL
AND V.DELETE_FLAG IS NULL
AND V.CLINIC_NAME IS NOT NULL
AND V.CLINIC -> CODE NOT IN (11, 68, 51)
AND V.SERVICE_CATEGORY NOT IN ('DAILY HOSP DATA', 'ANCILLARY PACKAGE DAILY', 'EVENT (HISTORICAL)', 'CHART REVIEW')
AND V.PATIENT_NAME -> DATE_OF_DEATH IS NULL
AND (
(TO_NUMBER(V.TRIBE_OF_MEMBERSHIP) > 1 AND TO_NUMBER(V.TRIBE_OF_MEMBERSHIP) < 968)
OR (TO_NUMBER(V.TRIBE_OF_MEMBERSHIP) IN (997, 999))
)
AND VAP.PATIENT_MERGED_TO IS NULL
) AS DISTINCTPatients
LEFT JOIN BMW.Problem AS P ON DISTINCTPatients.PATIENT_IEN = P.PATIENT_NAME
WHERE P.STATUS NOT IN ('DELETED')