title: ORDER BY issue on PLSQL
body: <p>I have set of data which should be fetched out and should prepare a report in the following order</p>
  
  <pre><code>Last Name      First Name      Location
  Blake          James           101007
  fusio          Wiko            101008 
                       100 Total
  
  FAROMOJU       BANKOLE         303315
  Gibbs          Rhonda          303315
  Greene         Leette          303315
                       331 Total
  
  ......
  ............
  </code></pre>
  
  <p>In that we can see that a <strong>3 letter code</strong> [extracted from the location code - <strong>CONCAT</strong>(SUBSTR(COLUMN3,3,3),'Total')] after all location set. I just figured out to get the report and it works wel except in the case of descending order of location code. It uses '<strong>location</strong>' to order by after making it into the 3 letter code instead of using the full number code. So in the result the descending order is not working well as I need. Please give me a solution,</p>
  
  <p>I am attaching my code,</p>
  
  <pre><code>******************************************************
  
  FUNCTION GET_ACTY_SUMMARY(V_STARTDATE IN DATE,V_ENDDATE IN DATE)
  RETURN TEMP_OUTPUT_TABLE PIPELINED IS
  CURSOR ACTY_SUMMARY IS
  
  /* The query fetches the report field grouped by First Name,Last Name
  and Location.*/
  
     SELECT
            COLUMN1 AS LASTNAME,
            COLUMN2 AS FIRSTNAME,
            COLUMN3 AS LOCATION,
            SUM(COLUMN4) AS ACTIVITYNM1,
            SUM(COLUMN5) AS ACTIVITYNM2,
            SUM(COLUMN6) AS ACTIVITYNM3,
            SUM(COLUMN7) AS ACTIVITYNM4,
            SUM(COLUMN8) AS ACTIVITYNM5,
            SUM(COLUMN9) AS REG_HRS,
            SUM(COLUMN10) AS OT_HRS,
            SUM(COLUMN11) AS TOTAL_HRS,
            SUM(COLUMN12) AS PRODUCTIVITY_PERCENTAGE
  
    FROM
    TABLE(ACTY_COLUMN(V_STARTDATE,V_ENDDATE))
    GROUP BY
     COLUMN1,
     COLUMN2,
     COLUMN3
  
  
    UNION
  
    SELECT NULL,
         NULL,
         CONCAT(SUBSTR(COLUMN3,3,3),'Total'),
         SUM(COLUMN4) AS ACTIVITYNM1,
         SUM(COLUMN5) AS ACTIVITYNM2,
         SUM(COLUMN6) AS ACTIVITYNM3,
         SUM(COLUMN7) AS ACTIVITYNM4,
         SUM(COLUMN8) AS ACTIVITYNM5,
         SUM(COLUMN9) AS REG_HRS,
         SUM(COLUMN10) AS OT_HRS,
         SUM(COLUMN11) AS TOTAL_HRS,
         SUM(COLUMN12) AS PRODUCTIVITY_PERCENTAGE
  
    FROM TABLE(ACTY_COLUMN(V_STARTDATE,V_ENDDATE))
    GROUP BY COLUMN3
    UNION
  
    SELECT NULL,
         NULL,
         'Total',
         SUM(COLUMN4) AS ACTIVITYNM1,
         SUM(COLUMN5) AS ACTIVITYNM2,
         SUM(COLUMN6) AS ACTIVITYNM3,
         SUM(COLUMN7) AS ACTIVITYNM4,
         SUM(COLUMN8) AS ACTIVITYNM5,
         SUM(COLUMN9) AS REG_HRS,
         SUM(COLUMN10) AS OT_HRS,
         SUM(COLUMN11) AS TOTAL_HRS,
         SUM(COLUMN12) AS PRODUCTIVITY_PERCENTAGE
  
    FROM TABLE(ACTY_COLUMN(V_STARTDATE,V_ENDDATE))
  
  
    ORDER BY 3 desc ;
  
  GAS ACTY_SUMMARY%ROWTYPE;
  
  /*Intialize the table output format with NULL.*/
  
  TT TEMP_OUTPUT_FORMAT := INITIALIZE_TABLE_FORMAT();
    BEGIN
    OPEN ACTY_SUMMARY;
    FETCH ACTY_SUMMARY INTO GAS;
      WHILE ACTY_SUMMARY%FOUND LOOP
        BEGIN
          TT.COLUMN1 := GAS.LASTNAME;
          TT.COLUMN2 := GAS.FIRSTNAME;
          TT.COLUMN3 := GAS.LOCATION;
          TT.COLUMN4 := GAS.ACTIVITYNM1;
          TT.COLUMN5 := GAS.ACTIVITYNM2;
          TT.COLUMN6 := GAS.ACTIVITYNM3;
          TT.COLUMN7 := GAS.ACTIVITYNM4;
          TT.COLUMN8 := GAS.ACTIVITYNM5;
          TT.COLUMN9 := GAS.REG_HRS;
         TT.COLUMN10 := GAS.OT_HRS;
         TT.COLUMN11 := GAS.TOTAL_HRS;
         TT.COLUMN12 := GAS.PRODUCTIVITY_PERCENTAGE;
         PIPE ROW(TT);
       END;
      FETCH ACTY_SUMMARY INTO GAS;
     END LOOP;
    CLOSE ACTY_SUMMARY;
    RETURN;
  END GET_ACTY_SUMMARY;
  
  END GEHC_ACTY_REPT_PKG;
  
  
  ******************************************************
  </code></pre>
  
  <p>Thanks
  Anand</p>