title: How to select from a list of columns? SQL Server
body: <p>I have a list of columns attained through a SQL query.</p>
  
  <pre><code>SELECT t.name AS table_name,
      SCHEMA_NAME(schema_id) AS schema_name,
      c.name AS column_name
  FROM sys.tables AS t
  INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
  ORDER BY table_name
  </code></pre>
  
  <p>Now for each column, I want to query some information about the column and put them together in a table.</p>
  
  <p>Here is the query I would run for a single column, for example.</p>
  
  <pre><code>Select Substring(col1, 1, 1) letter, Count(Substring(col1, 1, 1)) cnt
  From tbl
  Group By Substring(col1, 1, 1)
  </code></pre>
  
  <p>What this subquery does is that it attains the count of the first character of records in a column. So for example, there may be 6 records in col1 that start with the letter 'a', 15 records in col1 that start with the letter 'b', etc.</p>
  
  <p>How do I combine these two queries together into one table so I get something like...</p>
  
  <pre><code>Table_Name, Column_Name, letter, cnt
  
      tbl, col1, a, 6
  
      tbl, col1, b, 15
  
      tbl, col2, a, 5
  
      ...
  </code></pre>