r/SQLServer • u/74Yo_Bee74 • Jan 14 '26
Question Is there a way to quickly update a select statement to Group By, SUM and create Alias
Good day, all.
As the title states, I’m looking for a way to format a basic SELECT statement without grouping into one that includes grouping and SUM(), while keeping the alias name the same as the original column.
I’m guessing Query Designer is an option, but it still feels tedious to do so.
0
Upvotes
•
u/AutoModerator Jan 14 '26
After your question has been solved /u/74Yo_Bee74, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/taspeotis Jan 14 '26
ChatGPT, Copilot, Claude, Cursor, anything will do this easily.
4
u/mkt853 Jan 14 '26
Use a windowing function: SELECT ColumnA, SUM(ColumnB) OVER (PARTITION BY ColumnA)
1
u/SantaCruzHostel Jan 14 '26
SELECT A.myColumn, A.columnTwo FROM MyTable AS A WHERE A.anotherCokumn = 'foo'
1
u/dodexahedron 1 Jan 14 '26
Yes. But it is the query designer.
Any other tool I can think of is more work and not in the same workflow.
That is, unless you format your SQL such that you can make that kind of change with vertical selection or in an otherwise consistent format that can be used with find/replace, which now also has regexes in modern ssms.
1
u/thesqlguy Jan 14 '26
Many IDEs have macros that you might be able to set up to do this.
At the very least you can template out something like
SELECT <group col> , SUM(<sum col>) as <sum col> from <table> GROUP BY <group col>and you just provide values for those 3 params.You could also create a stored proc that does this for you using Dynamics SQL. So you can do something like
exec sp_group 'table', 'group column', 'sum column'but really not sure you are saving much typing.