Database reference guide |
HOME |
GROUP BY and PIVOT BYNot all NSQL statements will produce a Domain output. For example, to determine the number of customers of each sex in the database. The statement: SELECT Count(*) FROM Customer GROUP BY Sex; Here we are requesting a count of all records in the customer table, grouped by the Sex column, this NSQL statement produces a one dimensional crosstab, the sheet view of which appears as:
The customer table contains 5 females, 5 males and 1 record with a Null value for Sex. Such statements can also include a WHERE clause, to determine which records are to be included in the aggregation: SELECT Count(*) FROM Customer WHERE Age = 21 GROUP BY Sex; Will return:
In addition to counting the SELECT clause can be used to specify an aggregation of a numeric field. To determine the average age of customers of each distinct sex, the statement: SELECT MEAN(Age) FROM Customer Will return:
And SELECT SUM(Age) FROM Customer GROUP BY Sex; Will return:
Additionally the functions MAX and MIN are available and when used in the previous statement will return the maximum or minimum age for each gender in the customer table. The WHERE clause can, of course, be applied to restrict the records that are to be included in the calculation, for example: SELECT MIN(Age) FROM Customer WHERE Town = “CARDIFF” GROUP BY Sex; Will return:
A second dimension, or axis, can be added to the result set by applying the PIVOT BY clause after the GROUP BY clause: SELECT Count(*) FROM Customer GROUP BY Sex PIVOT BY Town; This statement will produce:
As before, functions of numeric fields can be substituted for counting and the WHERE clause can be used to select the records that should form part of the crosstabulation. The PIVOT BY clause can only be included after a GROUP BY clause, and each can only have one column. The SELECT clause can be followed by count (*) or a function of one column. The only functions against which GROUP BY is allowed are: Count, Sum, Mean, Avg, Min, Max, StDev and StDevs. |
Online & Instructor-Led Courses | Training Videos | Webinar Recordings | ![]() |
|
![]() |
© Alterian. All Rights Reserved. | Privacy Policy | Legal Notice | ![]() ![]() ![]() |