高分100分!求解决实际问题!给Sql语句添加参数!

wanglu_170858188 2012-07-24 11:19:27

进销存报表已经完成!我做成的没有参数,不能灵活的控制时间!


请大家给加个!

查询的条件是哪个月为本月
红线的StateDate为添加参数地方!目前那个是固定死的!

下面如果感觉不太符合常规!你可以加以改正!如果可以控制以为那个月份为本月即可!





/****** 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 StateDate<= DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1')) 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 ThisAdjustInQty --本月盘盈数量
,sum(t1.InQty+t1.SaleOutQty+t1.AdjustInQty)as ThisTotalInQty --本月入库合计数量

,sum(t1.SaleOutQty) as ThisSaleQty --本月销售数量
,sum(t1.PoRtnOutQty) as ThisPoRtnOutQty --本月退产数量
,sum(t1.AdjustOutQty) as ThisAdjustOutQty --本月盘亏数量
,sum(t1.IsGiftOutQty)as ThisIsGiftOutQty --本月赠品数量
,sum(t1.SaleOutQty+t1.PoRtnOutQty+t1.AdjustOutQty+t1.IsGiftOutQty)as ThisTotalOutQty --本月出库合计数量


into #t2 from Wms_StockDailyState as t1 left join V_Prod_Sku t2 on t1.SkuId=t2.SkuId
where StateDate>DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))
and StateDate<'2012-7-31 23:59:59'

这个是本月的
--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-07-30 23:59:59.000

group by t2.SkuNo,t2.ProductName

insert into #rep (SkuNo,ProductName,LastMonthAvgPrice,LastMonthCurrQty,LastMonthSumPrice,ThisMonthInQty,ThisMonthInPrice,ThisInSumPrice,
ThisSaleRtnQty,ThisAdjustInQty,ThisTotalInQty,ThisSaleQty,ThisPoRtnOutQty,ThisAdjustOutQty,ThisIsGiftOutQty,ThisTotalOutQty
)
select isnull(#t1.SkuNo,#t2.SkuNo),
isnull(#t1.ProductName,#t2.ProductName),
isnull(#t1.LastMonthAvgPrice,0),
isnull(#t1.LastMonthCurrQty,0),
isnull(#t1.LastMonthSumPrice,0),
isnull(#t2.ThisMonthInQty,0),
isnull(#t2.ThisMonthInPrice,0),
isnull(#t2.ThisInSumPrice,0),
isnull(#t2.ThisSaleRtnQty,0),
isnull(#t2.ThisAdjustInQty,0),
isnull(#t2.ThisTotalInQty,0), --本月入库合计数量
isnull(#t2.ThisSaleQty,0),
isnull(#t2.ThisPoRtnOutQty,0),
isnull(#t2.ThisAdjustOutQty,0) ,
isnull(#t2.ThisIsGiftOutQty,0),
isnull(#t2.ThisTotalOutQty,0) --本月出库合计数量


from #t1 full join #t2 on #t1.SkuNo = #t2.SkuNo


update #rep set

ThisMonthAvgPrice = isnull(case LastMonthCurrQty + ThisMonthInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0),2)end,0),--本月平均价格

ThisSaleRtnMoney = isnull(case ThisSaleRtnQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleRtnQty),2) end,0),--本月销售退回金额

ThisAdjustInMoney = isnull(case ThisAdjustInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustInQty),2) end,0),--本月盘盈金额

ThisTotalInMoney = isnull(case ThisTotalInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalInQty),2) end,0),--本月入库合计总金额

ThisSaleMoney = isnull(case ThisSaleQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleQty),2) end,0),--本月销售金额

ThisPoRtnOutMoney = isnull(case ThisPoRtnOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisPoRtnOutQty),2) end,0),--本月退产金额

ThisAdjustOutMoney = isnull(case ThisAdjustOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustOutQty),2) end,0),--本月盘亏金额

ThisIsGiftOutMoney = isnull(case ThisIsGiftOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisIsGiftOutQty),2) end,0),--本月赠送出库金额

ThisTotalOutMoney = isnull(case ThisTotalOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalOutQty),2) end,0),--本月出库合计总金额

--本月月结存数量
ThisMonthTotalQty = isnull(case(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) end,0),
--本月月结存金额
ThisMonthTotalMoney = case (LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*((LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty)),2) end



select * from #rep


GO











...全文
299 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
SQL code



ALTER proc [dbo].[P_Wms_StockMoneyState](
@t datetime //传入时间
)
as
begin
--利用@t 做查询
...


end
[/Quote]


恭喜得到2分!
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你在存储过程中添加时间参数就可以了。
[/Quote]


恭喜得到1分!
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你在存储过程中添加时间参数就可以了。
[/Quote]


恭喜得到1分!
天-笑 2012-07-24
  • 打赏
  • 举报
回复


ALTER proc [dbo].[P_Wms_StockMoneyState](
@t datetime //传入时间
)
as
begin
--利用@t 做查询
...


end


--小F-- 2012-07-24
  • 打赏
  • 举报
回复
你在存储过程中添加时间参数就可以了。
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 的回复:]
是的不错!你对JOB 调用熟悉吧!我开一新帖100分! 你来解决 行呗!
就是一个日结的存储过程 系统写一个游标循环调用!而且只能执行到系统前一天的日结!

你说明下你的需求
[/Quote]

http://topic.csdn.net/u/20120724/15/9a1eb0fe-e8ab-45c8-91df-faf84816b027.html
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复

magician547 获取20分恭喜牛人一下!不好意思给你的有点少!

sjyandgj 获取65分恭喜牛人一下!




一个日结的存储过程P_Wms_StockDailyState 参数是格式是'yyyy-MM-dd'

这个存储过程必须一天结算一次!比如在每天的几点几时几分执行这个存储过程!

现在呢 想把之前的也执行掉!意思就是

我从 2012-5-1 开始 执行到 今天之前的2012-7-23 设置1秒执行一次!

当执行到今天之前后停止! 后面 就是一天的某个时间 点执行一次!

不知道你能听明白吗?


Barton 2012-07-24
  • 打赏
  • 举报
回复
是的不错!你对JOB 调用熟悉吧!我开一新帖100分! 你来解决 行呗!
就是一个日结的存储过程 系统写一个游标循环调用!而且只能执行到系统前一天的日结!

你说明下你的需求
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 的回复:]
引用 28 楼 的回复:
引用 26 楼 的回复:
SQL code


ALTER proc [dbo].[P_Wms_StockMoneyState]
--0001 在这里添加参数
(
@datetime varchar(30)=''--格式可以为'2012-07-24 12:22:34' or '2012-07-24'
)
as
--0001 修改时间
declare……
[/Quote]
牛人!是的!太聪明了!
Barton 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 的回复:]
引用 26 楼 的回复:
SQL code


ALTER proc [dbo].[P_Wms_StockMoneyState]
--0001 在这里添加参数
(
@datetime varchar(30)=''--格式可以为'2012-07-24 12:22:34' or '2012-07-24'
)
as
--0001 修改时间
declare @cur_datetime……
[/Quote]



declare @datetime varchar(30)
set @datetime ='2012-04-21 12:33:22'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-04-21'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-04'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-4'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)

--输出:
2012-04-01 00:00:00.000 2012-05-01 00:00:00.000
2012-04-01 00:00:00.000 2012-05-01 00:00:00.000
2012-04-01 00:00:00.000 2012-05-01 00:00:00.000
2012-04-01 00:00:00.000 2012-05-01 00:00:00.000

参数是否合法,只需要在最前面进行相关的处理转化就可以了。
Barton 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 的回复:]
引用 26 楼 的回复:
SQL code


ALTER proc [dbo].[P_Wms_StockMoneyState]
--0001 在这里添加参数
(
@datetime varchar(30)=''--格式可以为'2012-07-24 12:22:34' or '2012-07-24'
)
as
--0001 修改时间
declare @cur_datetime……
[/Quote]



declare @datetime varchar(30)
set @datetime ='2012-04-21 12:33:22'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-04-21'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-04'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)
set @datetime ='2012-4'
SELECT @datetime=case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(case when len(@datetime)=7 or len(@datetime)=6 then @datetime+'-01' else @datetime end as datetime),120)+'1') end
select cast(@datetime as datetime),dateadd(month,1,@datetime)



楼主是这个意思么
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 的回复:]
楼主你统计 月初跟月末 算多余的啦
2012-04-01 00:00:00.000 2012-04-30 23:59:59.000
2012-06-01 00:00:00.000 2012-06-30 23:59:59.000

直接统计一个时间月初就可以了;2012-04-01 00:00:00.000
然后修改下你的查询条件
1.StateDate<'2012-04-01 00:0……
[/Quote]

是的不错!你对JOB 调用熟悉吧!我开一新帖100分! 你来解决 行呗!
就是一个日结的存储过程 系统写一个游标循环调用!而且只能执行到系统前一天的日结!
Barton 2012-07-24
  • 打赏
  • 举报
回复
楼主你统计 月初跟月末 算多余的啦
2012-04-01 00:00:00.000 2012-04-30 23:59:59.000
2012-06-01 00:00:00.000 2012-06-30 23:59:59.000

直接统计一个时间月初就可以了;2012-04-01 00:00:00.000
然后修改下你的查询条件
1.StateDate<'2012-04-01 00:00:00.000'--四月份以前的
2.StateDate>='2012-04-01 00:00:00.000' and StateDate<'2012-05-01 00:00:00.000'
--四月份的
这样简单明了
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 的回复:]
SQL code


ALTER proc [dbo].[P_Wms_StockMoneyState]
--0001 在这里添加参数
(
@datetime varchar(30)=''--格式可以为'2012-07-24 12:22:34' or '2012-07-24'
)
as
--0001 修改时间
declare @cur_datetime datetim……
[/Quote]

如果按照输入年月 怎么控制呀! 比如输入2012-07 判断跟日期的以前
孤独加百列 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 的回复:]

最后结贴问题:


当输入当输入'2012-5' 得到结果:
--获取五月最后一天时间
(1)2012-05-01
(2)2012-05-31
[/Quote]
直接判断月份就可以了,没必要比较日期
CONVERT(VARCHAR(7),StateDate,120) = CONVERT(VARCHAR(7),DATEADD(MONTH,-1,@Date),120)
Barton 2012-07-24
  • 打赏
  • 举报
回复

ALTER proc [dbo].[P_Wms_StockMoneyState]
--0001 在这里添加参数
(
@datetime varchar(30)=''--格式可以为'2012-07-24 12:22:34' or '2012-07-24'
)
as
--0001 修改时间
declare @cur_datetime datetime
SELECT @cur_datetime =case when @datetime = '' or @datetime is null then CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1')
else CONVERT(datetime,CONVERT(char(8),cast(@datetime as datetime),120)+'1') end

--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
---StateDate<= DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))
---0001修改为
StateDate<@cur_datetime
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 ThisAdjustInQty --本月盘盈数量
,sum(t1.InQty+t1.SaleOutQty+t1.AdjustInQty)as ThisTotalInQty --本月入库合计数量

,sum(t1.SaleOutQty) as ThisSaleQty --本月销售数量
,sum(t1.PoRtnOutQty) as ThisPoRtnOutQty --本月退产数量
,sum(t1.AdjustOutQty) as ThisAdjustOutQty --本月盘亏数量
,sum(t1.IsGiftOutQty)as ThisIsGiftOutQty --本月赠品数量
,sum(t1.SaleOutQty+t1.PoRtnOutQty+t1.AdjustOutQty+t1.IsGiftOutQty)as ThisTotalOutQty --本月出库合计数量


into #t2 from Wms_StockDailyState as t1 left join V_Prod_Sku t2 on t1.SkuId=t2.SkuId
where
----StateDate>DATEADD(Month,-1,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,getdate()),120)+'1'))
---and StateDate<'2012-7-31 23:59:59'
---0001修改为
StateDate>=@cur_datetime and StateDate<dateadd(month,1,@cur_datetime )

---这个是本月的
--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-07-30 23:59:59.000

group by t2.SkuNo,t2.ProductName

insert into #rep (SkuNo,ProductName,LastMonthAvgPrice,LastMonthCurrQty,LastMonthSumPrice,ThisMonthInQty,ThisMonthInPrice,ThisInSumPrice,
ThisSaleRtnQty,ThisAdjustInQty,ThisTotalInQty,ThisSaleQty,ThisPoRtnOutQty,ThisAdjustOutQty,ThisIsGiftOutQty,ThisTotalOutQty
)
select isnull(#t1.SkuNo,#t2.SkuNo),
isnull(#t1.ProductName,#t2.ProductName),
isnull(#t1.LastMonthAvgPrice,0),
isnull(#t1.LastMonthCurrQty,0),
isnull(#t1.LastMonthSumPrice,0),
isnull(#t2.ThisMonthInQty,0),
isnull(#t2.ThisMonthInPrice,0),
isnull(#t2.ThisInSumPrice,0),
isnull(#t2.ThisSaleRtnQty,0),
isnull(#t2.ThisAdjustInQty,0),
isnull(#t2.ThisTotalInQty,0), --本月入库合计数量
isnull(#t2.ThisSaleQty,0),
isnull(#t2.ThisPoRtnOutQty,0),
isnull(#t2.ThisAdjustOutQty,0) ,
isnull(#t2.ThisIsGiftOutQty,0),
isnull(#t2.ThisTotalOutQty,0) --本月出库合计数量


from #t1 full join #t2 on #t1.SkuNo = #t2.SkuNo


update #rep set

ThisMonthAvgPrice = isnull(case LastMonthCurrQty + ThisMonthInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0),2)end,0),--本月平均价格

ThisSaleRtnMoney = isnull(case ThisSaleRtnQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleRtnQty),2) end,0),--本月销售退回金额

ThisAdjustInMoney = isnull(case ThisAdjustInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustInQty),2) end,0),--本月盘盈金额

ThisTotalInMoney = isnull(case ThisTotalInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalInQty),2) end,0),--本月入库合计总金额

ThisSaleMoney = isnull(case ThisSaleQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleQty),2) end,0),--本月销售金额

ThisPoRtnOutMoney = isnull(case ThisPoRtnOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisPoRtnOutQty),2) end,0),--本月退产金额

ThisAdjustOutMoney = isnull(case ThisAdjustOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustOutQty),2) end,0),--本月盘亏金额

ThisIsGiftOutMoney = isnull(case ThisIsGiftOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisIsGiftOutQty),2) end,0),--本月赠送出库金额

ThisTotalOutMoney = isnull(case ThisTotalOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalOutQty),2) end,0),--本月出库合计总金额

--本月月结存数量
ThisMonthTotalQty = isnull(case(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) end,0),
--本月月结存金额
ThisMonthTotalMoney = case (LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*((LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty)),2) end



select * from #rep


GO



见0001部分
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复
最后结贴问题:


当输入当输入'2012-5' 得到结果:
--获取五月最后一天时间
(1)2012-05-01
(2)2012-05-31
wanglu_170858188 2012-07-24
  • 打赏
  • 举报
回复



ALTER proc [dbo].[P_Wms_StockMoneyState]
(
@Date VARCHAR(10) = ''
)
as


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 CONVERT(VARCHAR(6),StateDate,120) = CONVERT(VARCHAR(7),DATEADD(MONTH,-1,@Date),120) --上月
where StateDate<convert(datetime,convert(varchar(7),dateadd(month,0,@Date),120)+'-01')
group by t2.SkuNo,t2.ProductName




--本月的数据汇总到临时表#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 ThisAdjustInQty --本月盘盈数量
,sum(t1.InQty+t1.SaleOutQty+t1.AdjustInQty)as ThisTotalInQty --本月入库合计数量

,sum(t1.SaleOutQty) as ThisSaleQty --本月销售数量
,sum(t1.PoRtnOutQty) as ThisPoRtnOutQty --本月退产数量
,sum(t1.AdjustOutQty) as ThisAdjustOutQty --本月盘亏数量
,sum(t1.IsGiftOutQty)as ThisIsGiftOutQty --本月赠品数量
,sum(t1.SaleOutQty+t1.PoRtnOutQty+t1.AdjustOutQty+t1.IsGiftOutQty)as ThisTotalOutQty --本月出库合计数量


into #t2 from Wms_StockDailyState as t1 left join V_Prod_Sku t2 on t1.SkuId=t2.SkuId
--where CONVERT(VARCHAR(6),StateDate,120) = CONVERT(VARCHAR(6),@Date,120) --本月


where StateDate>=convert(datetime,convert(varchar(7),dateadd(month,0,@Date),120)+'-01') and
StateDate<DATEADD(Month,0,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,@Date),120)+'1'))


group by t2.SkuNo,t2.ProductName

insert into #rep (SkuNo,ProductName,LastMonthAvgPrice,LastMonthCurrQty,LastMonthSumPrice,ThisMonthInQty,ThisMonthInPrice,ThisInSumPrice,
ThisSaleRtnQty,ThisAdjustInQty,ThisTotalInQty,ThisSaleQty,ThisPoRtnOutQty,ThisAdjustOutQty,ThisIsGiftOutQty,ThisTotalOutQty
)
select isnull(#t1.SkuNo,#t2.SkuNo),
isnull(#t1.ProductName,#t2.ProductName),
isnull(#t1.LastMonthAvgPrice,0),
isnull(#t1.LastMonthCurrQty,0),
isnull(#t1.LastMonthSumPrice,0),
isnull(#t2.ThisMonthInQty,0),
isnull(#t2.ThisMonthInPrice,0),
isnull(#t2.ThisInSumPrice,0),
isnull(#t2.ThisSaleRtnQty,0),
isnull(#t2.ThisAdjustInQty,0),
isnull(#t2.ThisTotalInQty,0), --本月入库合计数量
isnull(#t2.ThisSaleQty,0),
isnull(#t2.ThisPoRtnOutQty,0),
isnull(#t2.ThisAdjustOutQty,0) ,
isnull(#t2.ThisIsGiftOutQty,0),
isnull(#t2.ThisTotalOutQty,0) --本月出库合计数量


from #t1 full join #t2 on #t1.SkuNo = #t2.SkuNo


update #rep set

ThisMonthAvgPrice = isnull(case LastMonthCurrQty + ThisMonthInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0),2)end,0),--本月平均价格

ThisSaleRtnMoney = isnull(case ThisSaleRtnQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleRtnQty),2) end,0),--本月销售退回金额

ThisAdjustInMoney = isnull(case ThisAdjustInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustInQty),2) end,0),--本月盘盈金额

ThisTotalInMoney = isnull(case ThisTotalInQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalInQty),2) end,0),--本月入库合计总金额

ThisSaleMoney = isnull(case ThisSaleQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisSaleQty),2) end,0),--本月销售金额

ThisPoRtnOutMoney = isnull(case ThisPoRtnOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisPoRtnOutQty),2) end,0),--本月退产金额

ThisAdjustOutMoney = isnull(case ThisAdjustOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisAdjustOutQty),2) end,0),--本月盘亏金额

ThisIsGiftOutMoney = isnull(case ThisIsGiftOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisIsGiftOutQty),2) end,0),--本月赠送出库金额

ThisTotalOutMoney = isnull(case ThisTotalOutQty when 0 then 0 else Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*(ThisTotalOutQty),2) end,0),--本月出库合计总金额

--本月月结存数量
ThisMonthTotalQty = isnull(case(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
(LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) end,0),
--本月月结存金额
ThisMonthTotalMoney = case (LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty) when 0 then 0 else
Round((LastMonthSumPrice + ThisInSumPrice)/nullif(LastMonthCurrQty + ThisMonthInQty,0)*((LastMonthCurrQty+ThisTotalInQty-ThisTotalOutQty)),2) end



select * from #rep


GO





--上月份
where StateDate<convert(datetime,convert(varchar(7),dateadd(month,0,@t),120)+'-01')


--本月分

where StateDate>=convert(datetime,convert(varchar(7),dateadd(month,0,@Date),120)+'-01') and
StateDate<DATEADD(Month,0,DATEADD(s,-1,CONVERT(char(8),DATEADD(Month,1,@Date),120)+'1'))




请大家看看 就是这个样子的!搞定了!谢谢大家的支持和帮助!
99归一 2012-07-24
  • 打赏
  • 举报
回复

declare @month_last varchar(20) --上月最一天
declare @month_this varchar(20) --本月最后一天
declare @dt varchar(20)
set @dt='2012-05-06'
set @month_last= convert(varchar(10),dateadd(day,-1,(CONVERT(char(8),@dt,120)+'1')),120)+' 23:59:59' --上月第一天
set @month_this=convert(varchar(10),DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@dt),120)+'1'),120)+' 00:00:00'--当月最后一天

select @month_last
select @month_this


--------------------
2012-04-30 23:59:59

(1 行受影响)


--------------------
2012-05-31 00:00:00

(1 行受影响)
playwarcraft 2012-07-24
  • 打赏
  • 举报
回复
按月来求的话,你要考虑日,时,分,秒干吗?直接比到“年月”不就可以了?

convert(char(06), getdate(), 112) 抓6码字符来比就可以了

上月

declare @input char(06)
set @input = '201207' --输入月份格式

--last month
convert(char(06),你的日期栏位,112) = convert(char(06), dateadd(month,-1,@input+'01') ,112)

--current month
convert(char(06),你的日期栏位,112) = @input
加载更多回复(15)
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于springboot+jpa实现java后台api接口,点餐系统源码+项目说明(毕设).zip # sell springboot实战入门,springboot+jpa实现java后台api接口 # 实现功能 - 1 购物车功能 - 2 java后台api接口 - 3 简单的电商订单后台 - 4 javaweb网站 - 5 微信小游戏体验 #### #### 项目中的sql.txt文档中是创建对应表格所需的sql语句 ## api接口说明文档 #### 一,获取pv访问量 - url:https://30paotui.com/pv/2048/list - 请方式:get - 返回的json数据如下 ``` { "code": 100, "msg": "成功", "data": 342 } ``` # 获取热销外卖产品 - url:https://30paotui.com/buyer/product/list - 请方式:get - 返回数据格式如下 ``` { "code": 100, "msg": "成功", "data": [ { "name": "热销", "type": 1, "foods": [ { "id": "5", "name": "奶茶", "price": 8, "desc": "奶茶妹妹嫁给了刘强东", "icon": null, "createTime": 1520823301000 } ] }, { "name": "包子类", "type": 3, "foods": [ { "id": "3", "name": "包子", "price": 4, "desc": "包子好吃", "icon": null, "createTime": 1513481102000 }, { "id": "4", "name": "蟹黄包", "price": 6, "desc": "蟹黄包比较贵", "icon": null, "createTime": 1513481642000 } ] } ] } ```

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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