Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it safe to remove when not part in this case statement?

The below Case Statement is part of an Select Query.

Is it safe to remove when not part in this case statement ?

Can the when not part removed ?
Will removing the when not , will have any difference ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

WHERE
CASE
WHEN o.order_id <> '0' THEN
  CASE
  WHEN (
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    1
  WHEN NOT (
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    0
  END
  ELSE CASE
  
  WHEN (
      (
        o.order_id = '0'
      )
      AND
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    1
  WHEN NOT (
      (
        o.order_id = '0'
      )
      AND
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    0
  END
END
= 1

>Solution :

You have a case when returning 1 and a when not (same criteria) to return 0. Case when defaults to null for scenarios that are not covered. So, if you remove the when not part, then the expression will evaluate to null instead of 0 and null=1 will filter out the records having such values. So, it seems that it will not be harmful to remove the when not part, but it will be less readable. I would use else 0 instead of the when not:

WHERE
CASE
WHEN o.order_id <> '0' THEN
  CASE
  WHEN (
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    1
    ELSE
    0
  END
  ELSE CASE
  
  WHEN (
      (
        o.order_id = '0'
      )
      AND
      a.acc_id IS NULL
      AND
      o.order_status = ‘a’
    )
    THEN
    1
    ELSE
    0
  END
END
= 1

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading