Generate SQL Statements to Group By Each Column of Table Separately

When working with data with the intent to visualize, there are times when I'll want to group by every column in a table, separately, one at a time, to determine the possible values in that column.

I do this often enough I created a simple SQL Query to build these group by statements for me:

declare @table_name varchar(200) = 'dbo.mytablename'
'select ' + + ', count(1) cnt from ' + @table_name + ' group by ' + + ' order by 2 '
from sys.columns c
where c.object_id = object_id(@table_name)

Hope this helps,


Popular posts from this blog

Search iPhone Text Messages with SQLite SQL Query

Use Azure Functions to Execute a SQL Azure Stored Procedure on a Timer

How to Turn Off Microsoft Arc Touch Mouse Scroll Sound Vibration