日期:2014-05-18 浏览次数:20613 次
/****** Object: StoredProcedure [dbo].[P_Wms_StockMoneyState] Script Date: 07/23/2012 14:50:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[P_Wms_StockMoneyState]
as
--declare @month_last varchar(20) --上月
--declare @month_this varchar(20) --本月
-- set @month_last = DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))
--set @month_this = Convert(nvarchar(20),Cast(getdate() AS DateTime),23)
create table #rep (Id int identity(1,1), --序列号
SkuNo varchar(60), --商品编号
ProductName varchar(100), --商品名称
LastMonthAvgPrice numeric(12,2) not null default 0, --上月平均单价
LastMonthCurrQty int not null default 0, --上月初数量
LastMonthSumPrice numeric(12,2) not null default 0, --上月初金额
ThisMonthInQty int not null default 0, --本月进货数量
ThisMonthInPrice numeric(12,2) not null default 0, --本月进货单价
ThisInSumPrice numeric(12,2) not null default 0, --本月进货金额
ThisMonthAvgPrice numeric(12,2) not null default 0, --本月平均单价
ThisSaleRtnQty int not null default 0, --本月销售退回数量
ThisSaleRtnMoney numeric(12,2) not null default 0, --本月销售退回金额
ThisAdjustInQty int not null default 0, --本月盘盈数量
ThisAdjustInMoney numeric(12,2) not null default 0, --本月盘盈金额
ThisTotalInQty int not null default 0, --本月入库合计数量
ThisTotalInMoney numeric(12,2) not null default 0, --本月入库合计金额
ThisSaleQty int not null default 0, --本月销售数量
ThisSaleMoney numeric(12,2) not null default 0, --本月销售金额
ThisPoRtnOutQty int not null default 0, --本月退产数量
ThisPoRtnOutMoney numeric(12,2) not null default 0, --本月退产金额
ThisAdjustOutQty int not null default 0, --本月盘亏数量
ThisAdjustOutMoney numeric(12,2) not null default 0, --本月盘亏金额
ThisIsGiftOutQty int not null default 0, --本月赠送出库数量
ThisIsGiftOutMoney numeric(12,2) not null default 0, --本月赠送出库金额
ThisTotalOutQty int not null default 0, --本月出库合计数量
ThisTotalOutMoney numeric(12,2) not null default 0, --本月出库合计金额
ThisMonthTotalQty int not null default 0, --本月月结存数量
ThisMonthTotalMoney numeric(12,2) not null default 0, --本月月结存金额
)
--上月的数据汇总到临时表#t1
select t2.SkuNo,t2.ProductName
,isnull(Sum(t1.BeginCostPrice*t1.BeginQty)/nullif(sum(t1.BeginQty),0),0)as LastMonthAvgPrice--上月平均单价
,Sum(t1.BeginQty)as LastMonthCurrQty --上月初数量,
,Sum(t1.BeginCostPrice*t1.BeginQty)as LastMonthSumPrice --上月初金额
into #t1 from Wms_StockDailyState as t1 left join V_Prod_Sku t2 on t1.SkuId=t2.SkuId
where [color=#FF0000] StateDate<= DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))[/color] group by t2.SkuNo,t2.ProductName
--StateDate<= DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate
--这个就是上月份的
测试select DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))
--2012-06-30 23:59:59.000
--本月的数据汇总到临时表#t2
select t2.SkuNo,t2.ProductName
,Sum(t1.InQty)as ThisMonthInQty --本月进货数量
,Sum(t1.InPrice)as ThisMonthInPrice --本月进货单价
,Sum(t1.InTaxAmt)as ThisInSumPrice --本月进货金额
,SUM(t1.SaleRtnInQty)as ThisSaleRtnQty --本月销售退回数量
,sum(t1.AdjustInQty)as ThisAdjust