21.10.2013 Views

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

GROUP BY<br />

DML statements<br />

Description: GROUP BY merges rows that have the same combination of values and/or NULLs in the item list<br />

into a single row. Any aggregate functions in the select list are applied to each group individually instead of<br />

to the dataset as a whole.<br />

Syntax:<br />

SELECT ... FROM ...<br />

GROUP BY [, ...]<br />

...<br />

::= column-name [COLLATE collation-name]<br />

| column-alias<br />

| column-position<br />

| expression<br />

• Only non-negative integer literals will be interpreted as column positions. If they are outside the<br />

range from 1 to the number of columns, an error is raised. Integer values resulting from expressions<br />

or parameter substitutions are simply invariables and will be used as such in the grouping. They<br />

will have no effect though, as their value is the same for each row.<br />

• A GROUP BY item cannot be a reference to an aggregate function (including one that is buried<br />

inside an expression) from the same context.<br />

• The select list may not contain expressions that can have different values within a group. To avoid<br />

this, the rule of thumb is to include each non-aggregate item from the select list in the GROUP BY<br />

list (whether by copying, alias or position).<br />

Note: If you group by a column position, the expression at that position is copied internally from the select list.<br />

If it concerns a subquery, that subquery will be executed at least twice.<br />

Grouping by alias, position and expressions<br />

Changed in: 1.0, 1.5, 2.0<br />

Description: In addition to column names, <strong>Firebird</strong> 2 allows column aliases, column positions and arbitrary<br />

valid expressions as GROUP BY items.<br />

Examples:<br />

These three queries all achieve the same result:<br />

select strlen(lastname) as len_name, count(*)<br />

from people<br />

group by len_name<br />

select strlen(lastname) as len_name, count(*)<br />

from people<br />

group by 1<br />

74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!