Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Could you please try the following:</p> <pre><code>SELECT Count(*) FROM yourtable where dateColumn between Now() and Now() - Interval 30 Day </code></pre> <p>It needs some looping, for a better answer to isolote all 30 days intervals going back. As you also need a 30 day interval between min(Date) in the table and the last loop date :) Or to the least another table that carries the dates of each 30 day interval, and then join.</p> <p>Here is getting count just by each calendar month. Not exactly what you need.</p> <pre><code>SELECT extract(month from datecolumn), count(*) FROM yourtable GROUP BY extract(month from datecolumn); </code></pre> <p>Given a thought to my latter comment and Stefan's comment, here is a long code yet with proper resutls. Based on my own sample data and compatible with MYSQL with <code>interval</code>. If you need to use with SQL Server please use <code>DateADD</code> or quivalent function.</p> <ul> <li><a href="http://sqlfiddle.com/#!2/a4093/3" rel="nofollow">SQLFIDDLE</a></li> </ul> <p>Sample data:</p> <pre><code>ID_MAIN FIELD1 FILTER ---------------------------------------- 1 red August, 05 2012 00:00:00+0000 2 blue September, 15 2012 00:00:00+0000 3 pink September, 20 2012 00:00:00+0000 4 blue September, 27 2012 00:00:00+0000 5 blue October, 02 2012 00:00:00+0000 6 blue October, 16 2012 00:00:00+0000 7 blue October, 22 2012 00:00:00+0000 8 pink November, 12 2012 00:00:00+0000 9 pink November, 28 2012 00:00:00+0000 10 pink December, 01 2012 00:00:00+0000 11 pink December, 08 2012 00:00:00+0000 12 pink December, 22 2012 00:00:00+0000 </code></pre> <p>Query:</p> <pre><code>set @i:= 0; SELECT MIN(filter) INTO @mindt FROM MAIN ; select count(a.id_main), y.dateInterval, (y.dateInterval - interval 29 day) as lowerBound from main a join ( SELECT date_format(Now(),'%Y-%m-%d') as dateInterval from dual union all select x.dateInterval from ( SELECT date_format( DATE(DATE_ADD(Now(), INTERVAL @i:=@i-29 DAY)),'%Y-%m-%d') AS dateInterval FROM Main, (SELECT @i:=0) r HAVING datediff(dateInterval,@mindt) &gt;= 30 order by dateInterval desc) as x) as y on a.filter &lt;= y.dateInterval and a.filter &gt; (y.dateInterval - interval 29 day) group by y.dateInterval order by y.dateInterval desc ; </code></pre> <p>Results:</p> <pre><code>COUNT(A.ID_MAIN) DATEINTERVAL LOWERBOUND ---------------------------------------------- 2 2012-12-30 2012-12-01 3 2012-12-01 2012-11-02 2 2012-11-02 2012-10-04 4 2012-10-04 2012-09-05 </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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