按照月份统计的汇总
2011-12-19 16:51
176 查看
declare @mouth int
declare @start nvarchar(20)
declare @end nvarchar(20)
declare @sql nvarchar(3000)
set @mouth=1
while @mouth<=12
begin
if @mouth=12
begin
set @start='2011-'+ltrim(rtrim(STR(@mouth)))+'-1'
set @end='2012-'+ltrim(rtrim(STR(1)))+'-1'
end
else
begin
set @start='2011-'+ltrim(rtrim(STR(@mouth)))+'-1'
set @end='2011-'+ltrim(rtrim(STR(@mouth+1)))+'-1'
end
if @mouth=1
begin
set @sql=N'select '+ltrim(rtrim(cast(@mouth as CHAR)))+' as ''月份'',count(1) as ''单数'',SUM(amount) as ''金额'' FROM [szjdw].[dbo].[mf_order] where cop_id=''DGJDW'' and createdt>'''+@start+''' and createdt<'''+@end+''''
end
else
begin
set @sql=@sql+' union '+'select '+ltrim(rtrim(cast(@mouth as CHAR)))+' as ''月份'',count(1) as ''单数'',SUM(amount) as ''金额'' FROM [szjdw].[dbo].[mf_order] where cop_id=''DGJDW'' and createdt>'''+@start+''' and createdt<'''+@end+''''
end
set @mouth=@mouth+1
end
select @sql
exec(@sql)
go
declare @start nvarchar(20)
declare @end nvarchar(20)
declare @sql nvarchar(3000)
set @mouth=1
while @mouth<=12
begin
if @mouth=12
begin
set @start='2011-'+ltrim(rtrim(STR(@mouth)))+'-1'
set @end='2012-'+ltrim(rtrim(STR(1)))+'-1'
end
else
begin
set @start='2011-'+ltrim(rtrim(STR(@mouth)))+'-1'
set @end='2011-'+ltrim(rtrim(STR(@mouth+1)))+'-1'
end
if @mouth=1
begin
set @sql=N'select '+ltrim(rtrim(cast(@mouth as CHAR)))+' as ''月份'',count(1) as ''单数'',SUM(amount) as ''金额'' FROM [szjdw].[dbo].[mf_order] where cop_id=''DGJDW'' and createdt>'''+@start+''' and createdt<'''+@end+''''
end
else
begin
set @sql=@sql+' union '+'select '+ltrim(rtrim(cast(@mouth as CHAR)))+' as ''月份'',count(1) as ''单数'',SUM(amount) as ''金额'' FROM [szjdw].[dbo].[mf_order] where cop_id=''DGJDW'' and createdt>'''+@start+''' and createdt<'''+@end+''''
end
set @mouth=@mouth+1
end
select @sql
exec(@sql)
go
相关文章推荐
- 按照月份来统计编号是1的大类的销售金额
- dedecms按照月份统计文章sql语句
- sql 按月份统计汇总排序
- mysql按照天、月份、年份统计查询
- SQLSERVER 使用 ROLLUP 汇总数据,实现分组统计,合计,小计
- 福利 | 资料下载_AI创业公司入选行业榜单统计汇总(7份榜单,315家公司)
- 强化练习2:编程实现统计某年某月份的天数
- pandas小记:pandas计算工具-汇总统计
- sql月份累加统计查询
- 【原创】分享一篇统计月份内的天数,工作天数、假日天数的脚本
- Excel VBA 根据日期汇总统计另一字段非重复值的个数
- 常用数据库查询统计汇总
- java按照月份把一个时间段拆分成多个时间区间
- 问题十三:按照气象划分法,通常以阳历3~5月为春季,6~8月为夏季,9~11月为秋季,12月~来年2月为冬季。使用switch结构编写函数根据月份输出对应的季节。
- sql T_sql 通用的动态按月份统计输出
- SQL按照日、周、月、年统计数据
- SQL按照日、周、月、年统计数据
- 如何对Excel 宏 按照数据库的方式统计分析
- 福利 | 资料下载_AI创业公司入选行业榜单统计汇总(7份榜单,315家公司)
- 关于假如有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?