Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grouping and column a liases??

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2011-12-01 : 19:20:10
Grouping doesn't allow the use of column aliases in the group by clause.

Does this mean that the expression that would be aliased would be calculated twice, once in the select and once in the Group By??

If that's the case, are there any suggestion for streamlining this kind of thing?

Thanks!

--PhB

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-01 : 19:26:05
quote:
Grouping doesn't allow the use of column aliases in the group by clause.

Yes. You have to use the column name or the full expression in the GROUP BY

quote:
Does this mean that the expression that would be aliased would be calculated twice, once in the select and once in the Group By??

No

quote:
If that's the case, are there any suggestion for streamlining this kind of thing?

use Derived table or CTE

select col_alias, sum(some_col)
from
(
select col1 + col2 + col3 as col_alias, some_col
from some_table
) as d
group by col_alias




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -