--建立数据
create table #tmp(日期 datetime,商品名称 varchar(10),数量 numeric(10,2))
insert into #tmp values('2016-1-1','a',1)
insert into #tmp values('2016-1-2','a',2)
insert into #tmp values('2016-1-3','b',3)
insert into #tmp values('2016-1-4','c',4)
insert into #tmp values('2016-1-5','d',5)
insert into #tmp values('2016-1-6','e',6)
insert into #tmp values('2016-1-7','f',7)
insert into #tmp values('2016-1-8','a',8)
insert into #tmp values('2016-1-9','a',9)
insert into #tmp values('2016-1-10','a',10)
insert into #tmp values('2016-1-11','b',11)
insert into #tmp values('2016-1-12','a',12)
insert into #tmp values('2016-1-13','c',13)
insert into #tmp values('2016-1-14','d',14)
insert into #tmp values('2016-1-15','f',15)
insert into #tmp values('2016-1-16','g',16)
insert into #tmp values('2016-1-17','h',17)
insert into #tmp values('2016-2-1','a',18)
insert into #tmp values('2016-2-2','a',2)
insert into #tmp values('2016-2-3','b',3)
insert into #tmp values('2016-2-4','c',3)
insert into #tmp values('2016-2-5','d',3)
insert into #tmp values('2016-2-6','a',4)
insert into #tmp values('2016-2-7','b',5)
insert into #tmp values('2016-2-8','b',6)
insert into #tmp values('2016-2-9','b',6)
insert into #tmp values('2016-2-10','c',6)
insert into #tmp values('2016-2-11','d',7)
insert into #tmp values('2016-2-12','e',7)
insert into #tmp values('2016-2-13','f',7)
--多余数据
insert into #tmp values('2016-3-01','h',70)
select 商品名称,
sum(case when year(日期)=2016 and month(日期)=1 then 数量 else 0 end) as '2016年1月',
sum(case when year(日期)=2016 and month(日期)=2 then 数量 else 0 end) as '2016年2月'
from #tmp
where 日期>='2016-01-01' and
日期<'2016-03-01' --只查2016年1月和2月的数据
group by 商品名称