Finance

Charts

Statistics

Macros

Search

SQL: Sorting Query Results in Excel VBA

You can influence the order of records returned by a query using the ORDER BY clause. You can specify one or multiple sorting keys. By default, sorting is in ascending order. To sort in descending order, use the keyword DESC.

Example 1: Sorting by Salary Descending

SQLCommand = « SELECT * FROM personen ORDER BY gehalt DESC »

  • The records are sorted in descending order based on the gehalt (salary) field, as shown in next Figure.

Example 2: Sorting by Last Name and First Name

SQLCommand = « SELECT * FROM personen ORDER BY name, vorname »

  • The records are sorted in ascending order by the name field (last name).
  • If multiple records share the same last name, they are further sorted by the vorname (first name) field, also ascending, as shown in next Figure.

Note:
To demonstrate this query more clearly, a temporary record (Maier, Wolfgang) was added.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx