数据统计年月加累计等问题,向高手请教,在线等待,小弟着急,请各位赐教,谢谢!

zjh_cr 2008-01-07 06:37:24
2007年1月份砂金入库金量
1月份 年累
一选厂 1.00 1.00
二选厂 2.00 2.00
三选厂 3.00 3.00
合计 6.00 6.00

2007年2月份砂金入库金量
2月份 年累
一选厂 4.00 5.00
二选厂 5.00 7.00
三选厂 6.00 9.00
合计 15.00 21.00

2007年3月份砂金入库金量
当月 年累
一选厂 7.00 12.00
二选厂 8.00 15.00
三选厂 9.00 18.00
合计 24.00 45.00
2007年4月份砂金入库金量
当月 年累
一选厂 10.00 22.00
二选厂 11.00 26.00
三选厂 12.00 30.00
合计 33.00 78.00
2007年5月份砂金入库金量
当月 年累
一选厂 13.00 35.00
二选厂 14.00 40.00
三选厂 15.00 45.00
合计 42.00 120.00
2007年6月份砂金入库金量
当月 年累
一选厂 16.00 51.00
二选厂 17.00 57.00
三选厂 18.00 63.00
合计 51.00 171.00
--------------------------------
如上面六个月的土所示,当我选择2007-1月的时候下面的表格里面就出现一月份的数据,我选择2007-2月的的时候将出现2月份的数据,请注意后面的年累,他是前面所有的月份的加上当前月份的!我选择2月时,年累也就是从1月份加到2月份,我选择4月份时候,就从1月累加到4月!
----
我在附件上面上传了个详细点的数据说明,和这个基本上差不多的!请 各位高手解答!谢谢!
...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt1314917 2008-01-08
  • 打赏
  • 举报
回复

加上排序:

alter proc wsp
@date varchar(7)
as
select 供货单位 ' ',月累,年累 from
(select 供货单位,
(select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4)
and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
from #t a group by 供货单位
union all
select distinct '合计' ' ',(select sum(原重g) from #t where datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4)
and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
from #t)k
order by replace(replace(replace(供货单位,'一',1),'二',2),'三',3)
zjh_cr 2008-01-08
  • 打赏
  • 举报
回复
老大 !怎么在上面排个序啊!就是一选厂,二选厂,三选厂,合计,就这样的排序!我弄了半天还是没弄出来!拜托在帮帮忙!
谢谢!各位!
zjh_cr 2008-01-08
  • 打赏
  • 举报
回复
谢谢 !谢谢!哈哈 !马上给分!
pt1314917 2008-01-08
  • 打赏
  • 举报
回复

哦。那改下:
create proc wsp
@date varchar(7)
as
select 供货单位 ' ',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4)
and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
from #t a group by 供货单位
union all
select distinct '合计' ' ',(select sum(原重g) from #t where datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4)
and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
from #t

exec wsp '2007-1'

这样就可以了`

zjh_cr 2008-01-08
  • 打赏
  • 举报
回复
背着灵魂漫步 大哥!你这个样作出来的 是年累计的汇总不变啊 !他 是一年的汇总,我想要的是年累汇总是这样的!
如果
exec wsp '2007-4'的话,那么年累里面的数据是前4个月的就是1到4月总共的数据汇总,月累是当月的数据。
例如下面的表结构:
-------------------------------------------
2007年1月份砂金入库金量
1月份 年累
一选厂 1.00 1.00
二选厂 2.00 2.00
三选厂 3.00 3.00
合计 6.00 6.00
-----------------------------------------
2007年2月份砂金入库金量
2月份 年累
一选厂 4.00 5.00
二选厂 5.00 7.00
三选厂 6.00 9.00
合计 15.00 21.00
----------------------------------------
看上面2月份的年累是一月份的年累加上2月年的当月的数据 5=4+1
-----------------------------------------
2007年3月份砂金入库金量
当月 年累
一选厂 7.00 12.00
二选厂 8.00 15.00
三选厂 9.00 18.00
合计 24.00 45.00
----------------------------------------
三月份的年累等于二月份的年累加上当月的:12=5+7;
也 就 是一月的年累加上二月的年累再加上当月的!12=1+4+7;以上列举的都是选厂1的!
-------------------------------------------------------------------------------
pt1314917 2008-01-07
  • 打赏
  • 举报
回复

不好意思。漏了合计。。。存储过程修改如下:

create proc wsp
@date varchar(7)
as
select 供货单位 ' ',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4))[年累]
from #t a group by 供货单位
union all
select distinct '合计' ' ',(select sum(原重g) from #t where datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4))[年累]
from #t

exec wsp '2007-1'
pt1314917 2008-01-07
  • 打赏
  • 举报
回复

create table #t (入库时间 datetime , 供货单位 varchar(10), 原重g float)
insert into #t values ('2007-1-1','一选厂',12 )
insert into #t values ('2007-1-1','三选厂',20 )
insert into #t values ('2007-1-1','二选厂',60 )
insert into #t values ('2007-1-4','一选厂',21 )
insert into #t values ('2007-2-1','三选厂',30 )
insert into #t values ('2007-2-1','二选厂',10 )

create proc wsp
@date varchar(7)
as
select 供货单位,
(select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
(select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4))[年累]
from #t a group by 供货单位

--1月份的
exec wsp '2007-1'

--2月份的
exec wsp '2007-2'
---依此类推。。
dobear_0922 2008-01-07
  • 打赏
  • 举报
回复
你的表结构贴出来看看,,,

34,575

社区成员

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

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