In this video, you will see how to sort the result set of a SELECT statement.
Objectives
After completing this lesson, you will be able to:
In this video, you will see how to sort the result set of a SELECT statement.
123456789101112131415161718
SELECT FROM /dmo/flight
FIELDS carrier_id,
connection_id,
flight_date,
seats_max - seats_occupied AS seats
WHERE carrier_id = 'AA'
AND plane_type_id = 'A320-200'
* ORDER BY seats_max - seats_occupied DESCENDING,
* flight_date ASCENDING
INTO TABLE @DATA(result).
out->write(
EXPORTING
data = result
name = 'RESULT'
).
Addition DISTINCT allows you to remove duplicates from the result set of a SELECT statement. The addition has to be placed right before the first element of the field list.
Watch this video to know how.
12345678910111213141516
SELECT FROM /dmo/connection
FIELDS
* DISTINCT
airport_from_id,
distance_unit
ORDER BY airport_from_id
INTO TABLE @DATA(result).
out->write(
EXPORTING
data = result
name = 'RESULT'
).
An aggregate expression calculates a single value from an sql_expression (sql_exp) by calling an aggregate function for multiple rows of a results set.
Some important aggregate functions are:
SUM( sql_exp )
Determines the sum of the results of the SQL expression sql_exp in a row set. The data type of the result set is the type of the SQL expression. The result type of the SQL expression sql_exp must be a numeric type.
MIN( sql_exp ) and MAX( sql_exp )
Determines the maximum value or minimum value of the results of the SQL expression sql_exp in a row set. The data type of the result is the type of the result of the SQL expression.
The result of the SQL expression sql_exp must be a numeric type. If a single column is specified for the expression, this column can also have any data type.
AVG( col [ AS dtype ] )
Determines the average value of the content of a column col in a row set. By default, the result type is FLTP. Optional addition AS dtype can be used to define the result type explicitly. The data type of the column must be numeric.
Note
SQL expressions cannot be specified as arguments for AVG.
COUNT( * ) or COUNT(*)
The two spellings have the same meaning and produce the number of rows in a row set. In most cases, the result type is INT4. The result type is INT8 if COUNT( * ) or COUNT(*) is the only element in the field list.
COUNT( DISTINCT sql_exp )
Determines the number of distinct values of the results of the SQL expression sql_exp in a row set. The data type of the result is INT4.
If the field list of a SELECT statement contains nothing but aggregate functions, the result set always consists of exactly one row. Note that this is also the case if the aggregation is based on an empty data set.
In our example, the SELECT statement on the left illustrates the data set on which the aggregations in the SELECT on right are based.
You can see that the maximum in column DISTANCE is 6217 while the minimum is 6162. The total number of rows is 4 and column AIRPORT_FROM_ID contains 3 distinct values (value 'FRA' appears twice).
1234567891011121314151617181920212223242526272829303132333435
SELECT FROM /dmo/connection
FIELDS carrier_id,
connection_id,
airport_from_id,
distance
WHERE carrier_id = 'LH'
INTO TABLE @DATA(result_raw).
out->write(
EXPORTING
data = result_raw
name = 'RESULT_RAW'
).
*********************************************************************
SELECT FROM /dmo/connection
FIELDS MAX( distance ) AS max,
MIN( distance ) AS min,
SUM( distance ) AS sum,
AVG( distance ) AS average,
COUNT( * ) AS count,
COUNT( DISTINCT airport_from_id ) AS count_dist
WHERE carrier_id = 'LH'
INTO TABLE @DATA(result_aggregate).
out->write(
EXPORTING
data = result_aggregate
name = 'RESULT_AGGREGATED'
).
If the field list of a SELECT statement contains a mixture of aggregate functions and other elements, for example, columns, expressions, and so on, the syntax of SQL requires a GROUP BY clause. The GROUP BY clause has to list all columns that appear in the field list outside of aggregations.
In the SELECT statement on the right, two columns are used in the field list: CARRIER_ID and DISTANCE. Column DISTANCE only appears as input for aggregate functions and therefore is not required after GROUP BY. But column CARRIER_ID is used outside of the aggregate functions and therefore has to be listed after GROUP BY.
The effect of GROUP BY can be see in the result of the SELECT statement. The data set, on which the aggregation is performed, is separated into subsets (groups) that have the same values in the GROUP BY - fields. The final result set contains one row for each group. The aggregate expressions are evaluated for each group separately.
For example, the row with CARRIER_ID = 'AA' contains COUNT = 5 and MAX = 4156 because the subset with CARRIER_ID = 'AA' contains 5 rows and within this subset the maximum value for distance is 4156.
123456789101112131415161718
SELECT FROM /dmo/connection
FIELDS
* carrier_id,
MAX( distance ) AS max,
MIN( distance ) AS min,
SUM( distance ) AS sum,
COUNT( * ) AS count
* GROUP BY carrier_id
INTO TABLE @DATA(result).
out->write(
EXPORTING
data = result
name = 'RESULT'
).
In this exercise, you sort the flights by date and calculate the average occupation of flights on the database.
Log in to track your progress & complete quizzes