mardi 14 janvier 2020

How to implement multiple if clauses using logical operator in Postgresql

I have 3 tables and I have to check the count for certain records as per below condition.

IF

count(T1DM DX = 0) & COUNT (T2DM DX = 0) & COUNT (T2DM MEDS >= 1) & COUNT(ABC > 6.5 OR DEF > 11.1 OR GHI > 7) >= 1 THEN "CASE"

ELSE

DO NOTHING

**OR** 

IF

count(T1DM DX = 0) & COUNT (T2DM DX >=1) & COUNT (T1DM MEDS = 0) & COUNT (T2DM MEDS = 0) & COUNT(ABC > 6.5 OR DEF > 11.1 OR GHI > 7) >= 1 THEN "CASE"

ELSE

DO NOTHING

My create table statements are given below

CREATE TABLE DX(
   DX_id  serial Primary Key
   person_id NOT NULL,
   condition_name VARCHAR (50) UNIQUE NOT NULL,
   cond_id VARCHAR (355) UNIQUE NOT NULL,
   condition_start_date TIMESTAMP NOT NULL,
   condition_end_date TIMESTAMP NOT NULL
);


CREATE TABLE RX(
   RX_id serial Primary Key
   person_id NOT NULL,
   presciption_name VARCHAR (50) UNIQUE NOT NULL,
   presc_id VARCHAR (355) UNIQUE NOT NULL,
   pres_start_date TIMESTAMP NOT NULL,
   pres_end_date TIMESTAMP 
);


CREATE TABLE Lab(
   Lab_id serial Primary Key
   person_id NOT NULL,
   Test_name VARCHAR (50) UNIQUE NOT NULL,
   Test_id VARCHAR (355) UNIQUE NOT NULL,
   Test_start_date TIMESTAMP NOT NULL,
   Test_value FLOAT
);

This is what I tried

IF ((select count(*) from public.DX where cond_id = 21) == 0 AND (select count(*) from public.DX where cond_id = 23) == 0 AND (select count(*) from public.RX where presc_id = 33) >=1
                                 AND (select count(*) from (select * from public.Lab where test_id in (41,42,43)) where test_value > 6.5)) THEN "FOUND"
ELSE
     DO NOTHING

But this throws error. Can anyone help me to implement multiple if clause with AND/OR logical operators in between them?

Points to Note

1) DX table can have multiple conditions. Based on the flowchart we need to look for T1DM and T2DM. We can identify them using cond_ids (T1DM - 21, T2DM - 23)

2) RX table can have multiple prescription of drugs. Based on flowchart we need to look for T1DM meds and T2DM meds. We can identify them using presc_ids (T1DM meds- 31, T2DM meds - 33)

3) Lab table can have multiple measurement/test results. Based on flowchart, abnormal lab is defined by threshold violation for any one of 3 measurements listed below.

 a) Three measurements - ABC, DEF, GHI. Test_ids for these three are 41,42,43
 b) ABC > 6.5
 c) DEF > 11.1
 d) GHI > 7

Aucun commentaire:

Enregistrer un commentaire