日期:2014-05-17  浏览次数:20626 次

跨年度的非自然月的报表的再次求教。
现有这样一个“物品领用明细表”Test,一共有5个字段:ID(自动编号),GetUseDate(领用时间),GetUseDept(领用部门),GoodsName(领用物品名称),GoodsQTY(领用数量)。其中“领用部门”的数量是固定的,只有5个部门:转运部、作业部、车队、仓储部、维修部。
现在想要做一个存储过程,通过设定四个参数:年份、起始月份、结束月份和领用物品名称,来查询制作如下格式的报表:
(注:月份为非自然月,存在跨年份的问题,比如2012年1月份的数据范围应为2011.11.26-2012.01.25)


请高手帮忙!!感谢!
------解决方案--------------------
exec pro_baobiao 2012,1,3,'柴油'
/*
月份/部门      转运部      作业部      车队        仓储部      维修部      合计
---------- ----------- ----------- ----------- ----------- ----------- -----------
1              33809       55852       49255       24111       5637        168664
2              32280       53435       46083       25273       5869        162940
3              32198       52052       47500       23901       6945        162596
合计           98287       161339      142838      73285       18451       494200
上年同期       101792      168729      145406      79538       18991       514456
同比           -3505       -7390       -2568       -6253       -540        -20256

*/

以下是存储过程脚本

GO
if object_id('pro_baobiao') is not null drop proc pro_baobiao
GO
/*
该过程为解CSDN帖而建
功能:格式化返回报表
参数: @year 年份
@BgnMonth 起始月份
@EndMonth 结束月份
@GoodsName 物料名称
作者:磊仔(tanleittl)
*/
CREATE PROC pro_baobiao
@year int,
@BgnMonth int,
@EndMonth int,
@GoodsName varchar(50)
AS
set nocount on

;with CET1 as
(
select cast(b.[month] as varchar(10))[月份/部门],a.GetUseDept,GoodsQTY  from test a, dbo.fn_monthlist(26) b 
where a.GetUseDate >= b.StartT and a.GetUseDate < b.EndT
and b.[year] = @year and  b.[month] >= @BgnMonth and  b.[month] <= @EndMonth and GoodsName = @GoodsName
)select *,[转运部] + [作业部] + [车队] + [仓储部] +&nbs