title: SQL Server how can I use COUNT DISTINCT(*) in HAVING clause?
body: <p>I have a procedure that counts all the unique [customerid] values and displays them in a SELECT list. I'm trying to sort the [customerid] where it is only "> 1" by using a HAVING clause, but SQL won't let me use the DISTINCT COUNT inside the HAVING. In my mind it makes sense that the HAVING should work with the COUNT but it does not: </p>
  
  <pre><code>USE MyCompany;
  GO
  SELECT DISTINCT COUNT(customerid) AS NumberOfOrdersMade, customerid AS 
  CustomerID
  FROM tblItems_Ordered
  GROUP BY customerid
  HAVING DISTINCT COUNT(customerid) &gt; 1
  GO
  </code></pre>