title: SQL. Wrong result of COUNT() function
body: <p>I can't find problem. I have many tables joined each other. I need to get count of form instances from <code>FormInstances</code> table (should count <code>Id</code> column), but It returing wrong data.</p>
  
  <p>So my query looks like:</p>
  
  <pre><code>DECLARE @dateStart  DATE        = NULL,
  @dateEnd        DATE            = NULL,
  @workshop       NVARCHAR(60)    = '',
  @shiftNr        NVARCHAR(60)    = '',
  @article        NVARCHAR(60)    = '',
  @controllier    NVARCHAR(60)    = 'Name of Controlier'
  
  SELECT COUNT(FI.Id) AS [Count of Instances]
  FROM FormInstances AS FI
  LEFT JOIN FormFields        AS FF
      ON FI.FormVersionId = FF.FormVersionid 
  LEFT JOIN .....
  WHERE
  FF.Id               =   FV.FormFieldId      AND
  FI.Id               =   ...
  </code></pre>
  
  <p>OUTPUT FOR NOW:</p>
  
  <pre><code>Count of Instances
       414400
       414400
       414400
       414400
       384800
  </code></pre>
  
  <p>OUTPUT SHOULD BE:</p>
  
  <pre><code>Count of Instances
         5
  </code></pre>
  
  <p>If I pass to <em>SELECT</em> clause: <code>FI.Id</code> (<code>Id</code> column from <code>FormInstances</code> table) in following:</p>
  
  <pre><code>SELECT COUNT(FI.Id) AS [Count of Instances],
               FI.Id  AS [Instance Ids]
  FROM....
  </code></pre>
  
  <p>I got correct <code>Instance Ids</code> but the same, wrong count of instances:</p>
  
  <pre><code>Count of Instances    Instance Ids
        414400              23
        414400              24
        414400              25
        414400              26
        384800              52
  </code></pre>
  
  <p>So why It returning incorrect count of instances?</p>