Database Toolbox    

Presenting Results in Specified Order

By default, the order of the rows in the query results depends on their order in the database, which is effectively random. Use the Order by field in Advanced query options to specify the order in which results appear. This example uses basic_where.qry, created and saved in Retrieving Information That Meets Specified Criteria. This example sorts the results of basic_where.qry, so that January is the primary sort field, February the secondary, and March the last. Results for January and February are ascending, and for March, are descending.

  1. Load basic_where.qry. For instructions, see Using a Saved Query.
  2. In Advanced query options, click Order by.

    The ORDER BY Clauses dialog box appears.

  1. For the Fields whose results you want to specify the order of, specify the Sort key number and Sort order. For example, specify January as the primary sort field, with results displayed in ascending order.
    1. From Fields, select January.
    2. For Sort key number, type 1.
    3. For Sort order, select Ascending.
    4. Click Apply.
  2. Specify February as the second sort field, with results displayed in ascending order.
    1. From Fields, select February.
    2. For Sort key number, type 2.
    3. For Sort order, select Ascending.
    4. Click Apply.
  3. Specify March as the third sort field, with results displayed in descending order.
    1. From Fields, select March.
    2. For Sort key number, type 3.
    3. For Sort order, select Descending.
    4. Click Apply.
  4. Click OK.

    The ORDER BY Clauses dialog box closes. The SQL statement in the Visual Query Builder reflects the order by clause you specified.

  1. Assign a MATLAB workspace variable, for example, B.
  2. Click Execute.
  3. To view the results, type B in the Command Window. Compare these to the unordered query results, shown as A.

    For B, results are first sorted by January sales, in ascending order, from 1200 for 400455 to 5000 for 400345.

    For items 400999, 400314, and 400876, January sales were equal at 3000. Therefore, the second sort key applies, February sales in ascending order, which were 1500, 2400, and 2400 respectively.

    For 400314 and 400876, February sales were both 2400, so the third sort key applies, March sales in descending order, which were 1800 and 1500 respectively.


 Grouping Criteria Creating Subqueries for Values from Multiple Tables