您的位置:首页 > 其它

按照月份统计的汇总

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: