SELECT  edr.id AS "Id"
    , edr.STATUS AS "Status"
    , edr.reject_reason AS "Rejection reason"
    , edr.access_code AS "Access code"
    , su.code AS "Subscription code"
    , of.code AS "Offer code"
    , edr.event_date AS "Event date"
    , edr.origin_batch AS "Origin batch"
    , edr.origin_record AS "Origin record"
    , edr.quantity AS "Quantity"
    , edr.parameter_1 AS "Param S1"
    , edr.parameter_2 AS "Param S2"
    , edr.parameter_3 AS "Param S3"
    , edr.parameter_4 AS "Param S4"
    , edr.parameter_5 AS "Param S5"
    , edr.parameter_6 AS "Param S6"
    , edr.parameter_7 AS "Param S7"
    , edr.parameter_8 AS "Param S8"
    , edr.parameter_9 AS "Param S9"
    , edr.date_parameter_1 AS "Param D1"
    , edr.date_parameter_2 AS "Param D2"
    , edr.date_parameter_3 AS "Param D3"
    , edr.date_parameter_4 AS "Param D4"
    , edr.date_parameter_5 AS "Param D5"
    , edr.decimal_parameter_1 AS "Param N1"
    , edr.decimal_parameter_2 AS "Param N2"
    , edr.decimal_parameter_3 AS "Param N3"
    , edr.decimal_parameter_4 AS "Param N4"
    , edr.decimal_parameter_5 AS "Param N5"  
from rating_edr edr
    INNER JOIN billing_subscription su  on su.id = edr.subscription_id
    INNER JOIN cat_offer_template of  on OF.id = su.offer_id where edr.event_date > (date_trunc('month', CURRENT_DATE) - CAST(:MONTH_INTERVAL || ' month' AS INTERVAL))  
and edr.STATUS ilike '%' || :EDR_STATUS || '%'  and(  su.code ilike '%' || :SEARCH_CRITERION || '%'  or OF.code ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.access_code ilike '%' || :SEARCH_CRITERION || '%'  or edr.reject_reason ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.origin_batch ilike '%' || :SEARCH_CRITERION || '%'  or edr.origin_record ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.parameter_1 ilike '%' || :SEARCH_CRITERION || '%'  or edr.parameter_2 ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.parameter_3 ilike '%' || :SEARCH_CRITERION || '%'  or edr.parameter_4 ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.parameter_5 ilike '%' || :SEARCH_CRITERION || '%'  or edr.parameter_6 ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.parameter_7 ilike '%' || :SEARCH_CRITERION || '%'  or edr.parameter_8 ilike '%' 
    || :SEARCH_CRITERION || '%'  or edr.parameter_9 ilike '%' || :SEARCH_CRITERION || '%')  
order by edr.STATUS, edr.id DESC