Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate SQL Statements to Group By Each Column of Table Separately
declare @table_name varchar(200) = 'dbo.mytablename'
select
'select ' + c.name + ', count(1) cnt from ' + @table_name + ' group by ' + c.name + ' order by 2 '
from sys.columns c
where c.object_id = object_id(@table_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment