你这样说
select year = datepart( year ,start_date ) ,
month = datepart(month , start_date) ,
N = count(distinct user_id)
from ...
where ...
group by datepart(year , start_date) ,
datepart(month , start_date)
order by 1,2
另一种技术使用一点点日期算术:
select start_date = dateadd(day ,
1-datepart(day,start_date) ,
convert(date,start_date)
) ,
N = count(distinct user_id)
from ...
where ...
group by dateadd(day, 1-datepart(day,start_date) , convert(date,start_date))
order by 1
如果表架构具有一个唯一的列—它要么表的主键或上有一个唯一索引列user_id
—您可以用distinct
关键字免除。
什么是start_date?一列agentdispodetail? – ASh 2015-01-20 19:47:30