94

I'm trying to perform a group by action on an aliased column (example below) but can't determine the proper syntax.

SELECT       LastName + ', ' + FirstName AS 'FullName'
FROM         customers
GROUP BY     'FullName'

What is the correct syntax?

Extending the question further (I had not expected the answers I had received) would the solution still apply for a CASEed aliased column?

SELECT       
    CASE
        WHEN LastName IS NULL THEN FirstName
        WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName
    END AS 'FullName'
FROM         customers
GROUP BY     
    LastName, FirstName

And the answer is yes it does still apply.

0

13 Answers 13

114

You pass the expression you want to group by rather than the alias

SELECT       LastName + ', ' + FirstName AS 'FullName'
FROM         customers
GROUP BY      LastName + ', ' + FirstName
Sign up to request clarification or add additional context in comments.

9 Comments

no need to put the ', ' in GROUP BY. the grouping can be indicated with or without the ', ', you can even avoid the typo (e.g. ','), so no invalid query error could arise
You should leave it in, at least to differentiate between 'x, yz' and 'xy, z' which would roll up to the same string without the comma.
@NXC: "GROUP BY LastName + FirstName" can fall into the trap you explain, but you can't group by that and have "LastName + ', ' + FirstName" in the SELECT. Using "GROUP BY LastName, FirstName", however, would be fine. (See my comment on the poster's question.)
+1 Dems GROUP BY Lastname, Firstname would be just fine. Explanation here ienablemuch.com/2010/04/some-wrong-voted-answer-on.html
@ConcernedOfTunbridgeWells After six years, I now realized where the confusion from my suggestion to remove the ', ' come from, when I said "just remove the ', ' in group by clause", what I mean is just remove the concatenation from the GROUP BY, and then just group things by their entity, which is lastname and firstname, and not removing the concatenation from SELECT; in fact I mentioned in second sentence what I wanted to achieve.
|
67

This is what I do.

SELECT FullName
FROM
(
  SELECT LastName + ', ' + FirstName AS FullName
  FROM customers
) as sub
GROUP BY FullName

This technique applies in a straightforward way to your "edit" scenario:

SELECT FullName
FROM
(
  SELECT
     CASE
       WHEN LastName IS NULL THEN FirstName
       WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName
     END AS FullName
  FROM customers
) as sub
GROUP BY FullName

1 Comment

Ah! From! I should have guessed. I was just trying to nest a Select inside a Select. Thanks
14

Unfortunately you can't reference your alias in the GROUP BY statement, you'll have to write the logic again, amazing as that seems.

SELECT       LastName + ', ' + FirstName AS 'FullName'
FROM         customers
GROUP BY     LastName + ', ' + FirstName

Alternately you could put the select into a subselect or common table expression, after which you could group on the column name (no longer an alias.)

Comments

9

Sorry, this is not possible with MS SQL Server (possible though with PostgreSQL):

select lastname + ', ' + firstname as fullname
from person
group by fullname

Otherwise just use this:

select x.fullname
from 
(
    select lastname + ', ' + firstname as fullname
    from person
) as x
group by x.fullname

Or this:

select lastname + ', ' + firstname as fullname
from person
group by lastname, firstname  -- no need to put the ', '

The above query is faster, groups the fields first, then compute those fields.

The following query is slower (it tries to compute first the select expression, then it groups the records based on that computation).

select lastname + ', ' + firstname as fullname
from person
group by lastname + ', ' + firstname

1 Comment

FWIW you can also use column aliases in GROUP BY in MySQL. But strictly, it isn't supported by the SQL standard.
4

You can use CROSS APPLY to create an alias and use it in the GROUP BY clause, like so:

SELECT       FullName
FROM         Customers
CROSS APPLY  (SELECT LastName + ', ' + FirstName AS FullName) Alias
GROUP BY     FullName

Comments

3

Given your edited problem description, I'd suggest using COALESCE() instead of that unwieldy CASE expression:

SELECT FullName
FROM (
  SELECT COALESCE(LastName+', '+FirstName, FirstName) AS FullName
  FROM customers
) c
GROUP BY FullName;

Comments

3

My guess is:

SELECT       LastName + ', ' + FirstName AS 'FullName'
FROM         customers
GROUP BY     LastName + ', ' + FirstName

Oracle has a similar limitation, which is annoying. I'm curious if there exists a better solution.

To answer the second half of the question, this limitation applies to more complex expressions such as your case statement as well. The best suggestion I've seen it to use a sub-select to name the complex expression.

5 Comments

no need to put the ', ' in GROUP BY. the grouping can be indicated with or without the ', ', you can even avoid the typo (e.g. ','), so no invalid query error could arise
True. But I find the above a touch more clear. Your sub-select solution is even clearer, obviously. ;-)
You need the comma to avoid the edge case 'ab, c' vs 'a,bc'. They would roll up the same without the comma.
@EricJ. What I mean on my first comment is remove the concatenation entirely from GROUP BY, not suggesting to merely remove the string ', ' and then keep the concatenation. There's no edge case even when you remove the concatenation entirely on GROUP BY. My first comment (merely remove the string ', ') on your answer is confusing, it looks like I'm suggesting to keep the concatenation on GROUP BY and just remove the string ', ', but that's not what I mean, sorry for that confusion. Slow performance of GROUP BY concatenation: ienablemuch.com/2010/04/some-wrong-voted-answer-on.html
Same answer as accepted answer, but this one has useful comments. Maybe add that in your answer to make your answer distinctive from the accepted one? and help people like me along when we are forced to work with legacy stuff.
1
SELECT       
    CASE
        WHEN LastName IS NULL THEN FirstName
        WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName
    END AS 'FullName'
FROM
    customers
GROUP BY     
    LastName,
    FirstName

This works because the formula you use (the CASE statement) can never give the same answer for two different inputs.

This is not the case if you used something like:

LEFT(FirstName, 1) + ' ' + LastName

In such a case "James Taylor" and "John Taylor" would both result in "J Taylor".

If you wanted your output to have "J Taylor" twice (one for each person):

GROUP BY LastName, FirstName

If, however, you wanted just one row of "J Taylor" you'd want:

GROUP BY LastName, LEFT(FirstName, 1)

Comments

0

If you want to avoid the mess of the case statement being in your query twice, you may want to place it in a User-Defined-Function.

Sorry, but SQL Server would not render the dataset before the Group By clause so the column alias is not available. You could use it in the Order By.

Comments

0

In the old FoxPro (I haven't used it since version 2.5), you could write something like this:

SELECT       LastName + ', ' + FirstName AS 'FullName', Birthday, Title
FROM         customers
GROUP BY     1,3,2

I really liked that syntax. Why isn't it implemented anywhere else? It's a nice shortcut, but I assume it causes other problems?

2 Comments

This is still possible but is generally frowned upon. Developers end up adding a new column to the middle of your column list and then unexpectedly changing the grouping criteria. It's one of those things that works but is considered poor form, like putting select * from in your queries.
@ShaneDelmore I know this is an ancient thread, but which engines support this syntax today? What is the term for this use? Can you use such non-aliases in other clauses (e.g. WHERE/HAVING)? If so, how does the engine know how to distinguish field #2 from the integer 2 (e.g. WHERE 2 is not NULL / HAVING 2 = 1)
0
SELECT 
CASE WHEN LastName IS NULL THEN FirstName         
     WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName     
END AS 'FullName' 
FROM  customers GROUP BY 1`

2 Comments

Check out this answer stackoverflow.com/a/497268/249353 to see why this way of grouping is not preferable.
Asked 3 years ago, accepted answer with 14 upvotes 3 years ago too.
0

For anyone who finds themselves with the following problem (grouping by ensuring zero and null values are treated as equals)...

SELECT AccountNumber, Amount AS MyAlias
FROM Transactions
GROUP BY AccountNumber, ISNULL(Amount, 0)

(I.e. SQL Server complains that you haven't included the field Amount in your Group By or aggregate function)

...remember to place the exact same function in your SELECT...

SELECT AccountNumber, ISNULL(Amount, 0) AS MyAlias
FROM Transactions
GROUP BY AccountNumber, ISNULL(Amount, 0)

Comments

0

Adding another variant on the answers here to say this is a great use case for Common Table Expressions (CTEs) as well.

WITH CustomersWithFullNames AS (
  SELECT LastName + ', ' + FirstName AS FullName, *
  FROM customers
)
SELECT FullName
FROM CustomersWithFullNames 
GROUP BY FullName

Performance wise, this will work the same as an inner subquery, but (to me at least) reads a little easier in a top down fashion

Side Note: Some databases like snowflake allow you to reference expressions in the projection clause in the group by clause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.