I have a very complicated SQL or at least it is to me the newbie.
I have 4 tables -- EVENT, ENTITY, PERSON_INVOLVED, GLOSSARY. The EVENT table is the main table. Each EVENT can have one or more PERSON_INVOLVED record. Each PERSON_INVOLVED record has one ENTITY record.
I need a view of the event, person_involved as patient, person_involved as visitor. Patient has the Pi_type_code of 238. Visitor has the pi_type_code of 240 AND glossary.system_table_name <> 'PHYSICIAN'
Here's the lastest SQL I have tried:
SELECT event.event_number, event.dttm_rcd_added,
EnPatient.first_name + ' ' + Enpatient.last_name AS PatientName,
EnVisitor.first_name + ' ' + Envisitor.last_name AS Visitor,
piVisitor.pi_type_code, system_table_name
FROM EVENT FULL OUTER JOIN
Person_involved AS PIPatient ON
(event.event_id = PIPatient.event_id AND
piPatient.pi_type_code = 238) FULL OUTER JOIN
entity AS EnPatient ON
piPatient.pi_eid = EnPatient.entity_id FULL OUTER JOIN
person_involved AS PIVisitor ON
(event.event_id = PiVisitor.event_id AND
PiVisitor.pi_type_code = 240) FULL OUTER JOIN
entity AS EnVisitor ON
PiVisitor.pi_eid = EnVisitor.entity_id FULL OUTER JOIN
glossary ON
(glossary.table_id = EnVisitor.entity_table_id AND
pivisitor.pi_type_code = 240 AND
glossary.system_table_name <> 'PHYSICIANS')
WHERE piPatient.pi_type_code = 238 AND
SUBSTRING(event.dttm_rcd_added, 1, 8)
>= CONVERT(char(26), DATEADD(dd, - 1, GETDATE()), 112) OR
(piVisitor.pi_type_code = 240 AND
glossary.system_table_name <> 'PHYSICIANS') AND
SUBSTRING(event.dttm_rcd_added, 1, 8)
>= CONVERT(char(26), DATEADD(dd, - 1, GETDATE()), 112)
This SQL results in
EV088994 20040225023929 Joshua Harmon MattGoldstein 240
EV088995 20040225031156 SHARON DENNISON
EV088997 20040225031507 RUTH STUMPF
EV088999 20040225031801 ROBERT FREEMAN
EV089000 20040225031925 DONALD STANKUS
The problem is MattGoldstein 240 has the system_Table_name of PHYSICIAN. I have tried other SQL's but this is the closest to what I need.
Any ideas how to keep the Matts out of the results?
Thanks
I have 4 tables -- EVENT, ENTITY, PERSON_INVOLVED, GLOSSARY. The EVENT table is the main table. Each EVENT can have one or more PERSON_INVOLVED record. Each PERSON_INVOLVED record has one ENTITY record.
I need a view of the event, person_involved as patient, person_involved as visitor. Patient has the Pi_type_code of 238. Visitor has the pi_type_code of 240 AND glossary.system_table_name <> 'PHYSICIAN'
Here's the lastest SQL I have tried:
SELECT event.event_number, event.dttm_rcd_added,
EnPatient.first_name + ' ' + Enpatient.last_name AS PatientName,
EnVisitor.first_name + ' ' + Envisitor.last_name AS Visitor,
piVisitor.pi_type_code, system_table_name
FROM EVENT FULL OUTER JOIN
Person_involved AS PIPatient ON
(event.event_id = PIPatient.event_id AND
piPatient.pi_type_code = 238) FULL OUTER JOIN
entity AS EnPatient ON
piPatient.pi_eid = EnPatient.entity_id FULL OUTER JOIN
person_involved AS PIVisitor ON
(event.event_id = PiVisitor.event_id AND
PiVisitor.pi_type_code = 240) FULL OUTER JOIN
entity AS EnVisitor ON
PiVisitor.pi_eid = EnVisitor.entity_id FULL OUTER JOIN
glossary ON
(glossary.table_id = EnVisitor.entity_table_id AND
pivisitor.pi_type_code = 240 AND
glossary.system_table_name <> 'PHYSICIANS')
WHERE piPatient.pi_type_code = 238 AND
SUBSTRING(event.dttm_rcd_added, 1, 8)
>= CONVERT(char(26), DATEADD(dd, - 1, GETDATE()), 112) OR
(piVisitor.pi_type_code = 240 AND
glossary.system_table_name <> 'PHYSICIANS') AND
SUBSTRING(event.dttm_rcd_added, 1, 8)
>= CONVERT(char(26), DATEADD(dd, - 1, GETDATE()), 112)
This SQL results in
EV088994 20040225023929 Joshua Harmon MattGoldstein 240
EV088995 20040225031156 SHARON DENNISON
EV088997 20040225031507 RUTH STUMPF
EV088999 20040225031801 ROBERT FREEMAN
EV089000 20040225031925 DONALD STANKUS
The problem is MattGoldstein 240 has the system_Table_name of PHYSICIAN. I have tried other SQL's but this is the closest to what I need.
Any ideas how to keep the Matts out of the results?
Thanks