不好意思! 上边的select COUNT(*) 应该为 sum(countNumber) ! 特此更正!
解决方案 »
- master..spt_values表的作用是什么,几个字段的含义是什么,谁能讲讲呢?
- 帮忙写个SQL,谢谢
- 求助,存储过程中如何给变量赋值?
- Access更新SqlServer2000表失败,提示操作必须使用一个可更新的查询,如何解决?
- 请教一个SQL Server管理问题
- 关于mssql中try和catch的错误提示问题
- datetime類型加1
- 如何远程连接sqlserver,提示是“不存在或访问被拒绝”
- Collation 的問題, 急急急!!!!!!!!!!!!!!!
- sql server2008的问题,触发器的,超难大侠帮忙
- 救命啊,不小心将mssql的的数据库“生成数据库脚本”又执行了一次,导致数据库的数据丢失,怎么恢复啊
- 蛙蛙请教:如何用一条sql语句遍历一个树型结构
select 日期,
类型A=sum(case type when A then CountNumber else 0 end),
类型B=sum(case type when B then CountNumber else 0 end),
类型C=sum(case type when C then CountNumber else 0 end)
From tableA
类型A=sum(case type when A then CountNumber else 0 end),
类型B=sum(case type when B then CountNumber else 0 end),
类型C=sum(case type when C then CountNumber else 0 end)
From tableA
group by convert(char(10),countDate,120)
select * into #temp from (select convert(char(10),countDate,120) as countDate,type,sum(countNumber) from tableA group by countDate,type) as R
你在來一個行列轉換
declare @sql varchar(8000)
set @sql = 'select countDate'
select @sql = @sql + ',sum(case type when '''+type+''' then c end)+'as '+type
from (select distinct type from #temp) as T
select @sql = @sql+' from #temp group by type'
exec(@sql)這樣寫不知道和樓主得效率誰高呢?望樓主測試後告訴一下