Group By



GROUP BY collapses the selected documents into one row for each distinct combination of the grouped fields or expressions. Fields that are not grouped can only be used inside Aggregate Functions.
When a query uses aggregate functions without GROUP BY, all of the selected documents form a single group. HAVING is not supported.

round-pushpin Groups the synonyms by source word and computes several aggregates.
SELECT
    SourceWordId,
    Min(TargetWordId) as Min,
    Max(TargetWordId) as Max,
    Sum(TargetWordId) as Sum,
    Avg(TargetWordId) as Avg,
    Count(TargetWordId) as Count
FROM
    WordList:Synonym
GROUP BY
    SourceWordId

round-pushpin Counts the words of each language, largest first.
SELECT
    LanguageId,
    Count(0) as Words
FROM
    WordList:Word
GROUP BY
    LanguageId
ORDER BY
    Count(0) DESC

round-pushpin Aggregates without GROUP BY.
SELECT Count(0) as Words, Min(Id) as FirstId, Max(Id) as LastId FROM WordList:Word




Home
The home page
SQL :: Aggregate Functions
Aggregate functions compute one value from a group of rows.
SQL :: Avg
Returns the average (arithmetic mean) of the values in each group.
SQL :: Count
Returns the number of values in each group. Count(0) and Count(*) count the documents in each group.
SQL :: CountDistinct
Returns the number of distinct values in each group. Values are compared without regard to case unless caseSensitive is true.
SQL :: Distinct
Removes duplicate rows from the results of a SELECT.
SQL :: Expressions
Literals, operators, comments and variables in KBSQL.
SQL :: GeometricMean
Returns the geometric mean of the values in each group: the nth root of their product. Useful for growth rates and ratios.
SQL :: Keywords
A breakdown of all of the statements and keywords supported by the query processor.
SQL :: Max
Returns the largest numeric value in each group. Use MaxString for text.