title: Get count of multiple column occurrences in SQL
body: <p>Have a question about a query I am attempting to write, using SQL Server 2012. I have a table with 2 columns (Agent and UN) I am focusing on. I would like to find the number off occurrences that Agent and UN occur. For example, in the table below:</p>
  
  <pre><code>+----+-------+----+
  | ID | Agent | UN |
  +----+-------+----+
  | 1  | 27    | 1  |
  | 2  | 28    | 1  |
  | 3  | 27    | 1  |
  | 4  | 27    | 2  |
  | 5  | 28    | 2  |
  | 6  | 28    | 2  |
  +----+-------+----+
  </code></pre>
  
  <p>An example would look similar to this:</p>
  
  <pre><code>+-------+--------+----+
  | Count | Agent  | UN |
  +-------+--------+----+
  |     2 |     27 |  1 |
  |     2 |     28 |  2 |
  |     1 |     27 |  2 |
  |     1 |     28 |  1 |
  +-------+--------+----+
  </code></pre>
  
  <p>The ID does not matter, I just want to get where Agent and UN occur as pairs and their counts.</p>
  
  <p>My attempt was this. I would have to do this for every agent and then combine them, but I'm thinking there is probably a quicker way to accomplish this? </p>
  
  <pre><code>select count(UN) as Count, Agent, UN 
  Where Agent = 27
  group by UN
  order by count(UN) desc
  </code></pre>