select (*) from ... where

(cc_flg NOT MATCHES '*[C-P]*')

AND
rd_flg MATCHES '*[123]*'

AND
dist_edge_ew > 10.0 AND dist_edge_ns > 10.0

AND 
(
 (   (rd_flg[1] != '0' AND (j_snr >= 7.0 OR j_msig <= 0.1551))
  OR (rd_flg[2] != '0' AND (h_snr >= 7.0 OR h_msig <= 0.1551))
  OR (rd_flg[3] != '0' AND (k_snr >= 7.0 OR k_msig <= 0.1551))
 )
 OR
 (
  (
   (rd_flg MATCHES '[1-3]??' AND rd_flg MATCHES '?[1-3]?' AND rd_flg MATCHES '??[1-3]') 
   AND 
   (    
       (j_snr >= 5.0 OR j_msig <= 0.2171)
    OR (h_snr >= 5.0 OR h_msig <= 0.2171)
    OR (k_snr >= 5.0 OR k_msig <= 0.2171)
   )
  )
 )
)

AND
( 
    ((rd_flg[1] > '0' AND rd_flg[1] != '2') OR (rd_flg[1] = '2' AND j_psfchi < 10.0))
 OR ((rd_flg[2] > '0' AND rd_flg[2] != '2') OR (rd_flg[2] = '2' AND h_psfchi < 10.0))
 OR ((rd_flg[3] > '0' AND rd_flg[3] != '2') OR (rd_flg[3] = '2' AND k_psfchi < 10.0))
)

AND
(
   rd_flg MATCHES '*[3]*'
   OR (rd_flg[1] = '2' AND j_m > 14.5) 
   OR (rd_flg[2] = '2' AND h_m > 14.0)
   OR (rd_flg[3] = '2' AND k_m > 13.5)
   OR ((ndet[2] BETWEEN 3 AND 4 AND ndet[1] >= 2) OR (ndet[2] > 4 AND ndet[1] >= 3))
   OR ((ndet[4] BETWEEN 3 AND 4 AND ndet[3] >= 2) OR (ndet[4] > 4 AND ndet[3] >= 3))
   OR ((ndet[6] BETWEEN 3 AND 4 AND ndet[5] >= 2) OR (ndet[6] > 4 AND ndet[5] >= 3))
   OR (rd_flg = '222' AND ((ndet[1] = 2 AND ndet[2] = 2) OR (ndet[3] = 2 AND ndet[4] = 2) OR (ndet[5] = 2 AND ndet[6] = 2)))
)

