一个比较难的sql语句,各位高手帮帮忙?

zhtflhs 2006-11-12 02:54:17
数据库表:
材料编号 材料名称 申请数量 申请日期(datetime)

要求生成的数据:
材料编号 材料名称 一月份的申请数量 二月份的申请数量 合计数量

实在是不知道怎么写,各位高手帮帮忙,谢谢了!
...全文
239 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhtflhs 2006-11-13
  • 打赏
  • 举报
回复
感谢各位网友的热心帮忙,谢谢了!结贴!
中国风 2006-11-12
  • 打赏
  • 举报
回复
楼主,在后面加合计就行了
create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)
insert ta
select '0001','木材',50,'2006-01-01 00:00:00' union all
select '0002','钢材',100,'2006-01-02 00:00:00' union all
select '0001','木材',200,'2006-01-03 00:00:00' union all
select '0001','木材',50,'2006-02-01 00:00:00' union all
select '0002','钢材',100,'2006-02-02 00:00:00' union all
select '0001','木头',200,'2006-02-03 00:00:00'
select 材料编号,材料名称
,[2006年第1月]=sum(case month(申请日期) when 1 then 申请数量 else 0 end),
[2006年第2月]=sum(case month(申请日期) when 2 then 申请数量 else 0 end),
[合计]=sum(申请数量)
from ta
where year(申请日期)=2006
group by 材料编号,材料名称

材料编号 材料名称 2006年第1月 2006年第2月 合计
----- ---------- ----------- ----------- -----------
0002 钢材 100 100 200
0001 木材 250 50 300
0001 木头 0 200 200

(所影响的行数为 3 行)
xyxfly 2006-11-12
  • 打赏
  • 举报
回复
:)
marco08 2006-11-12
  • 打赏
  • 举报
回复
select 材料编号, 材料名称,
一月份的申请数量=sum(case when month(申请日期)=1 then 申请数量 end),
二月份的申请数量=sum(case when month(申请日期)=2 then 申请数量 end),
合计数量=sum(申请数量)
from ta
group by 材料编号, 材料名称
jackeyabc 2006-11-12
  • 打赏
  • 举报
回复
declare @sql varchar(4000)
set @sql=''
select @sql=@sql+',[2006年第'+convert(varchar,月份)+'月]=sum(case month(申请日期) when '
+convert(varchar,月份)+' then 申请数量 else 0 end)'
from (select 月份=month(申请日期) from ta where year(申请日期)=2006 group by month(申请日期))a
--print @sql
select @sql='select 材料编号 '+@sql+'from ta where year(申请日期)=2006 group by 材料编号'
exec(@sql)
中国风 2006-11-12
  • 打赏
  • 举报
回复
动态,静态都有了,楼主可以结贴了
declare @sql varchar(4000)
set @sql=''
select @sql=@sql+',[2006年第'+convert(varchar,月份)+'月]=sum(case month(申请日期) when '
+convert(varchar,月份)+' then 申请数量 else 0 end)'
from (select 月份=month(申请日期) from ta where year(申请日期)=2006 group by month(申请日期))a
--print @sql
select @sql='select 材料编号,材料名称 '+@sql+'from ta where year(申请日期)=2006 group by 材料编号,材料名称'
exec(@sql)
--动态
select 材料编号,材料名称
,[2006年第1月]=sum(case month(申请日期) when 1 then 申请数量 else 0 end),
[2006年第2月]=sum(case month(申请日期) when 2 then 申请数量 else 0 end)
from ta
where year(申请日期)=2006
group by 材料编号,材料名称
--静态
材料编号 材料名称 2006年第1月 2006年第2月
----- ---------- ----------- -----------
0002 钢材 100 100
0001 木材 250 50
0001 木头 0 200
中国风 2006-11-12
  • 打赏
  • 举报
回复
declare @sql varchar(4000)
set @sql=''
select @sql=@sql+',[2006年第'+convert(varchar,月份)+'月]=sum(case month(申请日期) when '
+convert(varchar,月份)+' then 申请数量 else 0 end)'
from (select 月份=month(申请日期) from ta where year(申请日期)=2006 group by month(申请日期))a
--print @sql
select @sql='select 材料编号 '+@sql+'from ta where year(申请日期)=2006 group by 材料编号'
exec(@sql)
材料编号 2006年第1月 2006年第2月
----- ----------- -----------
0001 250 250
0002 100 100
删除测试表
--drop table ta
中国风 2006-11-12
  • 打赏
  • 举报
回复
create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)
insert ta
select '0001','木材',50,'2006-01-01 00:00:00' union all
select '0002','钢材',100,'2006-01-02 00:00:00' union all
select '0001','木材',200,'2006-01-03 00:00:00' union all
select '0001','木材',50,'2006-02-01 00:00:00' union all
select '0002','钢材',100,'2006-02-02 00:00:00' union all
select '0001','木头',200,'2006-02-03 00:00:00'

select 材料编号
,[2006年第1月]=sum(case month(申请日期) when 1 then 申请数量 else 0 end),
[2006年第2月]=sum(case month(申请日期) when 2 then 申请数量 else 0 end)
from ta
where year(申请日期)=2006
group by 材料编号

材料编号 2006年第1月 2006年第2月
----- ----------- -----------
0001 250 250
0002 100 100

(所影响的行数为 2 行)
hhhdyj 2006-11-12
  • 打赏
  • 举报
回复
日起的判断可以具体一点儿,比如申请日期 BETWEEN '2006/1/1' AND '2006/1/31'
SELECT 材料编号, 材料名称,
SUM(CASE WHEN DATEPART(MONTH,申请日期) = 1 THEN 申请数量 ELSE 0 END) AS 一月份的申请数量,
SUM(CASE WHEN DATEPART(MONTH,申请日期) = 2 THEN 申请数量 ELSE 0 END) AS 二月份的申请数量,SUM(申请数量) AS 合计数量
FROM 数据库表
aaronslee 2006-11-12
  • 打赏
  • 举报
回复
合计就直接 sum ,偶忘记写了
aaronslee 2006-11-12
  • 打赏
  • 举报
回复

Select 材料编号,材料名称,sum(case when month(申请日期)=1 then 申请数量 else 0) as 一月份的申请数量,
sum(case when month(申请日期)=2 then 申请数量 else 0) as 二月份的申请数量,
sum(case when month(申请日期)=3 then 申请数量 else 0) as 三月份的申请数量,
sum(case when month(申请日期)=4 then 申请数量 else 0) as 四月份的申请数量,
sum(case when month(申请日期)=5 then 申请数量 else 0) as 五月份的申请数量,
sum(case when month(申请日期)=6 then 申请数量 else 0) as 六月份的申请数量,
sum(case when month(申请日期)=7 then 申请数量 else 0) as 七月份的申请数量,
sum(case when month(申请日期)=8 then 申请数量 else 0) as 八月份的申请数量,
sum(case when month(申请日期)=9 then 申请数量 else 0) as 九月份的申请数量,
sum(case when month(申请日期)=10 then 申请数量 else 0) as 十月份的申请数量,
sum(case when month(申请日期)=11 then 申请数量 else 0) as 十一月份的申请数量,
sum(case when month(申请日期)=12 then 申请数量 else 0) as 十二月份的申请数量,
from 表
where year(申请日期)=2006
group by 材料编号,材料名称
xyxfly 2006-11-12
  • 打赏
  • 举报
回复
贴点数据出来

34,590

社区成员

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

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