Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That's because you have no records in <code>BILLS</code> for <code>BILLDATE = '2013-09-02'</code> (I suppose that <code>WHERE BILLDATE &gt;='08/01/2013' AND BILLDATE&lt;='09/09/2013'</code> is a typo and you wanted to write <code>WHERE BILLDATE &gt;='01/09/2013' AND BILLDATE&lt;='03/09/2013'</code></p> <p>There're many DB specific methods to make your query, but I think if you have to do this types of query often, the best way to go is to create static <code>DATES</code> table, populate it with dates (from, for example <code>1900-01-01</code> till <code>3000-01-01</code>):</p> <pre><code>Date 1900-01-01 1900-01-02 1900-01-03 ... 2010-01-01 ... </code></pre> <p>and use it like:</p> <pre><code>select d.DATE, count(b.BILLDATE) as cnt from DATES as d left outer join BILLS as b on b.BILLDATE = d.DATE where d.DATE &gt;='2013-09-01' and d.DATE &lt;= '2013-09-03' group by d.DATE </code></pre> <p>Note also <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> format for dates.</p> <p>If you don't care much about performance, you can use <a href="http://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx" rel="nofollow">recursive common table expression</a>:</p> <pre><code>with cte([Date]) as ( select convert(date, '20130901', 112) -- Start date union all select dateadd(dd, 1, [Date]) from cte where [Date] &lt; '20130903' -- End date ) select c.[Date], count(b.BILLDATE) as cnt from cte as c left outer join BILLS as b on b.BILLDATE = d.DATE </code></pre>
 

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