日期
2011-01-06
2011-02-06
2011-03-06
2011-04-06
2011-05-06
2011-06-06
2011-06-07
2011-06-08
2011-06-092012-01-01
2012-01-02
2012-01-03
(执行SQL语句后)
我想显示出来是这样: 年份 1月 2月 3月 4月 5月 6月 ~12月
2011 1 1 1 1 1 4
2012 3 0 0 0 0 0
怎么实现
2011-01-06
2011-02-06
2011-03-06
2011-04-06
2011-05-06
2011-06-06
2011-06-07
2011-06-08
2011-06-092012-01-01
2012-01-02
2012-01-03
(执行SQL语句后)
我想显示出来是这样: 年份 1月 2月 3月 4月 5月 6月 ~12月
2011 1 1 1 1 1 4
2012 3 0 0 0 0 0
怎么实现
解决方案 »
- 有关SQL2000中去除空格的问题
- 高分求助一个SQL问题
- SQL 怎样递归查询出树状结构数据过滤掉没有父结点的?
- 120分急救!sql语句,根据排序字段值,分组排序实现?
- ==同一个sqlserver服务器上,2个数据库怎样联系?
- 如果在安装SQLSERVER2K时选择NT身份验证,现在是否可以将其改成混和验证?
- 这个存储过程该怎么写?
- 急……,不小心把数据库在管理器中给删除了,没有选择保留备份,怎么能够将数据给找回来??能行马上给分!!!!
- 一个简单的SQL SERVER2000的问题?????
- 为SqlServer 添加一个登陆账号,这个账号可以修改自己的密码应该赋予什么权限
- 跪求解答!!sql server2000读取不出数据?
- 求助一个建立存储过程的小问题
declare @T table (日期 datetime)
insert into @T
select '2011-01-06' union all
select '2011-02-06' union all
select '2011-03-06' union all
select '2011-04-06' union all
select '2011-05-06' union all
select '2011-06-06' union all
select '2011-06-07' union all
select '2011-06-08' union all
select '2011-06-09' union all
select '2012-01-01' union all
select '2012-01-02' union all
select '2012-01-03'select
年份=year(日期),
[1月]=sum(case when month(日期)=1 then 1 else 0 end),
[2月]=sum(case when month(日期)=2 then 1 else 0 end),
[3月]=sum(case when month(日期)=3 then 1 else 0 end),
[4月]=sum(case when month(日期)=4 then 1 else 0 end),
[5月]=sum(case when month(日期)=5 then 1 else 0 end),
[6月]=sum(case when month(日期)=6 then 1 else 0 end),
[7月]=sum(case when month(日期)=7 then 1 else 0 end),
[8月]=sum(case when month(日期)=8 then 1 else 0 end),
[9月]=sum(case when month(日期)=9 then 1 else 0 end),
[10月]=sum(case when month(日期)=10 then 1 else 0 end),
[11月]=sum(case when month(日期)=11 then 1 else 0 end),
[12月]=sum(case when month(日期)=12 then 1 else 0 end)
from @T group by year(日期)
/*
年份 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2011 1 1 1 1 1 4 0 0 0 0 0 0
2012 3 0 0 0 0 0 0 0 0 0 0 0
*/