SELECT Year,
       Period,
       Company,
       Coordinator,
       Coordinator_Name,
       count(distinct Order_No) "No of Orders",
       count(distinct Order_No || line_no || REL_NO) "No of Lines",
       
       SUM(NET_CURR_AMOUNT) as Revenue
  from (SELECT EXTRACT(YEAR FROM cs.INVOICE_DATE) Year,
               EXTRACT(MONTH FROM cs.INVOICE_DATE) Period,
               cs.company,
               CUSTOMER_ORDER_API.Get_Authorize_Code(cs.ORDER_NO) Coordinator,
               ORDER_COORDINATOR_API.Get_Name(CUSTOMER_ORDER_API.Get_Authorize_Code(cs.ORDER_NO)) Coordinator_Name,
               cs.ORDER_NO,
               cs.NET_CURR_AMOUNT,
               ol.line_no,
               ol.REL_NO
          FROM CUST_ORD_INVO_STAT cs
         INNER JOIN CUSTOMER_ORDER_LINE ol
            ON cs.order_no = ol.order_no
           AND cs.line_no = ol.line_no
           AND cs.REL_NO = ol.REL_NO
           AND cs.line_item_no = ol.line_item_no
        
         WHERE (TRUNC(cs.INVOICE_DATE) >=
               TO_DATE('&Valid_From', 'MM/DD/YYYY') AND
               TRUNC(cs.INVOICE_DATE) <= TO_DATE('&Valid_To', 'MM/DD/YYYY'))
           AND cs.CONTRACT like
               NVL('&COMPANY', '%')
           AND CUST_ORD_CUSTOMER_API.Get_Cust_Grp(ol.CUSTOMER_NO) LIKE
               NVL('&CUST_GRP', '%')) it
 GROUP BY year, Period, Company, Coordinator, Coordinator_Name
 ORDER BY year, Period

 -- Lov reference is removeed due to limitations in IFS cloud 