Home » SQL & PL/SQL » SQL & PL/SQL » SQL query (SQL developer, Oracle Clinical)
SQL query [message #606114] Mon, 20 January 2014 01:04 Go to next message
draroda
Messages: 1
Registered: January 2014
Location: Mumbai
Junior Member

I have two tables called "Adverse Event" and "Medication" as below.

Adverse Event :
Patient Event
100 Fever
100 Cold
100 Blood Pressure

Patient Medicine Event
100 PCM Fever
100 Rantac Acidity
100 domstal Vomit
100 Storvas Blood Pressure.

I want to create query based on logic that "If any event is recorded in the "Adverse Event" table then atleast one record of same event with same patient should also present in the "Medication" table.

According to that, my final output should look like as below.

Final output:
Patient Event
100 Cold

I can do the same in Oracle clinical with the help of $HASDATA, but i dont know if it is possible in SQL query and how.

Regards,
Dr.Rajesh
Re: SQL query [message #606121 is a reply to message #606114] Mon, 20 January 2014 01:25 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
I'd say that (at least) one table is missing - the one which joins EVENT and MEDICINE EVENT. There's no way to return "100 Cold" according to data you provided so far.
Re: SQL query [message #606122 is a reply to message #606114] Mon, 20 January 2014 01:28 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hello,

It's your first post, I suggest from next time please provide the SQL DDL statements so that anybody that execute it and provide you the solution. I am doing it this time for you :

SQL> DROP TABLE ADVERSE_EVENT;
 
Table dropped
SQL> DROP TABLE MEDICATION;
 
Table dropped
SQL> CREATE TABLE Adverse_Event(Patient number, Event VARCHAR2(20));
 
Table created
SQL> INSERT INTO Adverse_Event values(100, 'Fever');
 
1 row inserted
SQL> INSERT INTO Adverse_Event values(100, 'Cold');
 
1 row inserted
SQL> INSERT INTO Adverse_Event values(100, 'Blood Pressure');
 
1 row inserted
SQL> CREATE TABLE medication(Patient number, Medicine varchar2(20),Event VARCHAR2(20));
 
Table created
SQL> INSERT INTO medication VALUES(100, 'PCM', 'Fever');
 
1 row inserted
SQL> INSERT INTO medication VALUES(100, 'Rantac', 'Acidity');
 
1 row inserted
SQL> INSERT INTO medication VALUES(100, 'domstal', 'Vomit');
 
1 row inserted
SQL> INSERT INTO medication VALUES(100, 'Storvas', 'Blood Pressure');
 
1 row inserted
SQL> COMMIT;
 
Commit complete
SQL> SELECT *
  2    FROM ADVERSE_EVENT
  3   WHERE EVENT NOT IN (SELECT EVENT FROM MEDICATION);
 
   PATIENT EVENT
---------- --------------------
       100 Cold
Re: SQL query [message #606124 is a reply to message #606122] Mon, 20 January 2014 01:37 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Ah, NOT IN! I misread (actually, misunderstood) the question. Sorry.
Re: SQL query [message #606128 is a reply to message #606114] Mon, 20 January 2014 01:54 Go to previous message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Assuming both "adverse" columns can't be NULL:
select * from adverse
where (patient, event) not in 
      (select patient, event from medication)
/

select * from adverse a
where not exists (select null from medication m
                  where m.patient = a.patient
                    and m.event = a.event)
/

Previous Topic: / (slash) character after END keyword
Next Topic: Rank without analytical function
Goto Forum:
  


Current Time: Tue Apr 23 02:40:28 CDT 2024