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(Revenue) as "Sales Value (CURR)"

  from (SELECT EXTRACT(YEAR FROM cs.DATE_ENTERED) Year,
               EXTRACT(MONTH FROM cs.DATE_ENTERED) 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,
               ol.line_no,
               ol.REL_NO,
               Customer_Order_Line_API.Get_Sale_Price_Incl_Tax_Total(ol.ORDER_NO,
                                                                     ol.LINE_NO,
                                                                     ol.REL_NO,
                                                                     ol.LINE_ITEM_NO) as Revenue
        
          FROM CUSTOMER_ORDER cs
         INNER JOIN CUSTOMER_ORDER_LINE ol
            ON cs.order_no = ol.order_no
        
         WHERE (TRUNC(cs.DATE_ENTERED) >=
               TO_DATE('&Valid_From', 'MM/DD/YYYY') AND
               TRUNC(cs.DATE_ENTERED) <= TO_DATE('&Valid_To', 'MM/DD/YYYY'))
           AND cs.CONTRACT like NVL('&CONTRACT', '%')
           AND CUST_ORD_CUSTOMER_API.Get_Cust_Grp(cs.CUSTOMER_NO) LIKE
               NVL('&CUST_GRP', '%')) it
 GROUP BY year, Period, Company, Coordinator, Coordinator_Name
 ORDER BY year, Period
