你這樣說
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