title: SQL selecting count of maximal value for each id
body: <p>I am struggling with selecting count of maximal value of the status for each author.
  I have a log database that is saving every change to a ticket - from sending, accepting to solving.</p>
  <p>Data is being uploaded by stored procedures.</p>
  <p>When someone sends a ticket it receives status 0, when someone accepts it gets 1, when it's getting done it's getting 2 and when it's declined it gets 5.
  I want to get statistics for employees on how many tickets they have done, how much they have accepted etc.</p>
  <p>So it would be the maximal value for each ticket_id.</p>
  <p><strong>Database looks like this:</strong></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Ticket_ID</th>
  <th style="text-align: center;">Author of Changes</th>
  <th style="text-align: right;">Ticket Status</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">176</td>
  <td style="text-align: center;">ANDRZEJ POLEROWICZ</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">176</td>
  <td style="text-align: center;">Piotr Wierzbicki</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">175</td>
  <td style="text-align: center;">ANDRZEJ POLEROWICZ</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">175</td>
  <td style="text-align: center;">Miroslaw Patyk</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">174</td>
  <td style="text-align: center;">Sebastian Wiechec</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">173</td>
  <td style="text-align: center;">ANDRZEJ POLEROWICZ</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">173</td>
  <td style="text-align: center;">Mariusz Maly</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">172</td>
  <td style="text-align: center;">ANDRZEJ POLEROWICZ</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">172</td>
  <td style="text-align: center;">Mateusz Gawlowski</td>
  <td style="text-align: right;">0</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>There is also time_stamp for each addition of a new row to the table.</p>
  <p>Database sorted on ticket_id desc and status desc</p>
  <p><a href="https://i.stack.imgur.com/QS2bZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QS2bZ.png" alt="enter image description here" /></a></p>
  <p>I know that I have to count the max of status for each ticket_id but I don't really know how to make it properly.</p>
  <pre><code>select autor_of_changes, count(ticket_status) as statusiki from aa_ticketing_app_log 
  group by autor_of_changes
  order by statusiki desc, autor_of_changes desc
  </code></pre>
  <p>I had no other idea, but this one gives me an error :</p>
  <blockquote>
  <p>Cannot perform an aggregate function on an expression containing an
  aggregate or a subquery.</p>
  </blockquote>
  <p>would love to get some help or tips on it, Thanks a lot!</p>