Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't recommend the <code>HAVING</code> keyword for newbies, it is <a href="http://www.dcs.warwick.ac.uk/~hugh/TTM/HAVING-A-Blunderful-Time.html" rel="nofollow">essentially for legacy purposes</a>. </p> <p>The following is more verbose but could be easier to understand and therefore maintain (I've used a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" rel="nofollow">CTE</a> for the table <code>CUSTOMER_RENOVATION_TALLIES</code> but it could be a <a href="http://msdn.microsoft.com/en-us/library/ms187956.aspx" rel="nofollow"><code>VIEW</code></a> :</p> <pre><code>WITH CUSTOMER_RENOVATION_TALLIES ("Customer-ID", Tally) AS ( SELECT [Customer-ID], COUNT(*) AS Tally FROM RENOVATION GROUP BY [Customer-ID] ) SELECT c."Customer-ID", c.name, r.Tally FROM CUSTOMER AS c INNER JOIN CUSTOMER_RENOVATION_TALLIES AS r ON c."Customer-ID" = r."Customer-ID"; </code></pre> <p>If you want to include a tally of zero for customers without renovations then <code>UINON</code> this set to the above resultset e.g. </p> <pre><code>WITH CUSTOMER_RENOVATION_TALLIES ("Customer-ID", Tally) AS ( SELECT [Customer-ID], COUNT(*) AS Tally FROM RENOVATION GROUP BY [Customer-ID] ) SELECT c."Customer-ID", c.name, r.Tally FROM CUSTOMER AS c INNER JOIN CUSTOMER_RENOVATION_TALLIES AS r ON c."Customer-ID" = r."Customer-ID" UNION SELECT c."Customer-ID", c.name, 0 AS Tally FROM CUSTOMER AS c WHERE NOT EXISTS ( SELECT * FROM CUSTOMER_RENOVATION_TALLIES AS r WHERE c."Customer-ID" = r."Customer-ID" ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload