Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Created March 15, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronhoffman/d49d3705fd716b0fe6cb to your computer and use it in GitHub Desktop.
Save aaronhoffman/d49d3705fd716b0fe6cb to your computer and use it in GitHub Desktop.
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