Home > Database > Mysql Tutorial > How to GROUP BY Aliased Columns in SQL Server?

How to GROUP BY Aliased Columns in SQL Server?

Susan Sarandon
Release: 2025-01-24 10:22:11
Original
156 people have browsed it

How to GROUP BY Aliased Columns in SQL Server?

Use the other names in SQL Server for the group by operation

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template