答案 0 :(得分:0)
一种方法是生成日期并使用横向联接来计算卖家数量:
with dates as (
select cast('2017-06-01' as date) as dte
union all
select dateadd(day, 1, dte)
from dates
where dte < '2017-06-30'
)
select d.dte, t.num_sellers
from dates d outer apply
(select count(distinct t.seller_id) as num_sellers
from t
where t.transaction_date > dateadd(day, -30, d.dte) and
t.transaction_date <= d.dte
) t
order by d.dte;