老手请进,帮忙解决,分数不够可以再送(在线)

Lyer 2003-01-15 10:52:58
比如有一张表A,有以下几个字段
定单号:A_ord
定单日期:A_date
定单量:A_mnt
然后用一条SQL语句,按某一年十二个月统计出各月的定单量,格式如下:

月 份: 200301 200302 200303 ......
定单量: ... ... ... ......
...全文
51 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-01-15
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select '

select @sql = @sql + 'sum(case convert(char(6),A_date,112) when '''+[temp]+'''
then a_mnt else 0 end) as '''+[temp]+''','
from (select distinct convert(char(6),A_date,112) [temp] from 表A where year(A_date)=@你的年如2002) as a

select @sql = left(@sql,len(@sql)-1) + ' from 表A'

exec(@sql)
go
pengdali 2003-01-15
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select '

select @sql = @sql + 'sum(case convert(char(6),A_date,112) when '''+[temp]+'''
then a_mnt else 0 end) as '''+[temp]+''','
from (select distinct convert(char(6),A_date,112) [temp] from 表A where year(A_date)=@你的年如2002) as a

select @sql = left(@sql,len(@sql)-1) + ' from 表A'

print @sql
exec(@sql)
go
DainelLee 2003-01-15
  • 打赏
  • 举报
回复
不好意思,还是有些问题
select sum(case when a_date=1 then a_mnt else 0 end) as 1月
sum(case when a_date=2 then a_mnt else 0 end) as 2月
.
.
end) as 12月
from table
Lyer 2003-01-15
  • 打赏
  • 举报
回复
谢谢DainelLee(Iinexperience)
DainelLee 2003-01-15
  • 打赏
  • 举报
回复
select sum(case when a_date=1 then a_mnt else 0 end) as 1月
when a_date=2 then a_mnt else 0 end) as 2月
.
.
end) as 12月
from table
DainelLee 2003-01-15
  • 打赏
  • 举报
回复
你变变就可以用了
DainelLee 2003-01-15
  • 打赏
  • 举报
回复
select sum(case when a_date=1 then a_mnt else 0
when a_date=2 then a_mnt else 0
.
.
end)
from table

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧