Home > database >  Postgres Case statement then null
Postgres Case statement then null

Time:01-06

The below query is part of a select query which I have got in my application

where 
case when o.tracking_id <> '0' then a.account_id is null
and 
o.status = 'N'

else
(

o.tracking_id = '0'
and 
o.status = 'N'

)
END

I am having hardtime to understand the below line

Can you please tell me what does this exactly mean ?

case when o.tracking_id <> '0' then a.account_id is null

CodePudding user response:

I wouldn't use a CASE expression here but instead would use the following logic:

WHERE
    (o.tracking_id <> '0' AND a.account_id IS NULL AND o.status = 'N')
    OR
    (o.tracking_id = '0' AND o.status = 'N')

CodePudding user response:

The condition is written somewhat clumsily. It is equivalent to this more readable expression:

where (o.tracking_id = '0' or a.account is null)
and o.status = 'N';
  • Related