日期:2014-05-17 浏览次数:20484 次
ID Name Indate
1 张三 2012-1-02 00:00:00
2 张三 2012-2-02 00:00:00
3 张三 2012-3-02 00:00:00
4 张三 2012-4-02 00:00:00
5 张三 2012-5-02 00:00:00
6 张三 2012-5-02 00:00:00
7 张三 2012-7-02 00:00:00
8 张三 2012-8-02 00:00:00
9 张三 2012-9-02 00:00:00
10 张三 2013-1-02 00:00:00
1月 1
2月 1
3月 1
4月 1
5月 2
6月 0
7月 1
8月 1
9月 1
10月 0
11月 0
12月 0
with tb(
ID , Name , Indate)as (
select 1, '张三', '2012-1-02 00:00:00' union all
select 2, '张三', '2012-2-02 00:00:00' union all
select 3, '张三', '2012-3-02 00:00:00' union all
select 4, '张三', '2012-4-02 00:00:00' union all
select 5, '张三', '2012-5-02 00:00:00' union all
select 6, '张三', '2012-5-02 00:00:00' union all
select 7, '张三', '2012-7-02 00:00:00' union all
select 8, '张三', '2012-8-02 00:00:00' union all
select 9, '张三', '2012-9-02 00:00:00' union all
select 10, '张三', '2013-1-02 00:00:00')
select number,isnull(b,0) from (select number from master..spt_values
where type='p' and number between 1 and 12)a
left join (select month(indate)a,count(1)b from tb
where year(indate)=2012 group by month(indate))b
on&nb