メールアドレスとそれらのメールアドレスがテーブルに追加された日付のデータセットがあります。さまざまな日付の電子メールアドレスの複数のエントリが存在する可能性があります。たとえば、以下のデータセットがある場合。その日付から3日前までの間にある個別の電子メールの日付と数を取得したいと考えています。
Date | email
-------+----------------
1/1/12 | test@test.com
1/1/12 | test1@test.com
1/1/12 | test2@test.com
1/2/12 | test1@test.com
1/2/12 | test2@test.com
1/3/12 | test@test.com
1/4/12 | test@test.com
1/5/12 | test@test.com
1/5/12 | test@test.com
1/6/12 | test@test.com
1/6/12 | test@test.com
1/6/12 | test1@test.com
3の期間を使用すると、結果セットは次のようになります。
date | count(distinct email)
-------+------
1/1/12 | 3
1/2/12 | 3
1/3/12 | 3
1/4/12 | 3
1/5/12 | 1
1/6/12 | 2
以下のクエリを使用して日付範囲の明確なカウントを取得できますが、日ごとの範囲のカウントを取得しようとしているため、数百の日付の範囲を手動で更新する必要はありません。
select test.date, count(distinct test.email)
from test_table as test
where test.date between '2012-01-01' and '2012-05-08'
group by test.date;