title: MySQL - count in same table
body: <p>I have the following table:</p>
  
  <pre><code>+----+---------------------+---------------+
  | id |     created_at      | deklaracja_id |
  +----+---------------------+---------------+
  |  1 | 2015-01-09 12:14:00 |             1 |/*deklaracja*/
  |  2 | 2015-02-09 12:14:00 |             1 |/*korekta for 1*/
  |  3 | 2015-03-09 12:14:00 |             3 |/*deklaracja/
  |  4 | 2015-01-09 12:14:00 |             3 |/*korekta for 3*/
  |  5 | 2015-10-09 12:14:00 |             3 |/*korekta for 3*/
  |  6 | 2015-10-09 12:14:00 |             6 |/*deklaracja*/
  +----+---------------------+---------------+
  </code></pre>
  
  <p>Cond:
  id = deklaracja_id is "deklaracja"
  id &lt;> deklaracja_id is "korekta"</p>
  
  <p>I need a query to show all "deklaracja" and count of their "korekty" later than 2015-01-09.</p>
  
  <pre><code>Ex.
  +----+---------------------+---------------+
  | id |     created_at      | korekty_count |
  +----+---------------------+---------------+
  |  1 | 2015-01-09 12:14:00 |             1 |
  |  3 | 2015-03-09 12:14:00 |             2 |
  |  6 | 2015-10-09 12:14:00 |             0 |
  +----+---------------------+---------------+
  </code></pre>
  
  <p>I've tried something like:</p>
  
  <pre><code>SELECT *, 
  SUM(CASE WHEN (id &lt;&gt; deklaracja_id)THEN 1 ELSE 0 END ) 
  AS korekty_count 
  FROM Deklaracja 
  WHERE created &gt;= '2015-09-01 00:00:00'
  </code></pre>
  
  <p>but it's not working and now I'm totally stuck :/</p>