In SQL Server, perform a specific syntax for performing the Group By operation on the other names. The following is the necessary adjustment:
Do not quote aliases in the group by clause (for example, Group by 'Fullname'), but should use the expression of defining aliases itself. For example, if the aliases are defined as LastName 'and' FirstName, the correct group by expression is:
This modified query correctly applies the group by operation based on the calculated "FullName 'expression, so as to group the results.
<code class="language-sql">SELECT LastName + ', ' + FirstName AS 'FullName' FROM customers GROUP BY LastName + ', ' + FirstName</code>
Extension: Case expression
The same principle is also applicable to the beyond all of the derivatives from the Case expression. Consider the following example:
Here, the group by clause lists LastName and FIRSTNAME because they are included in Case expressions for derivatives 'Fullname' aliases. This ensures that the result is based on the basic value used to calculate the basic value used to calculate the 'Fullname'.The above is the detailed content of How to GROUP BY Aliased Columns in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!