Sql server 2008 存储过程中的高级折扣系统
我正在为每月销售制作一个存储过程。在存储过程中,我们有折扣。这个折扣可以从三个不同的表中获得。如果折扣不在id.rabatt中,则应从dp.rabatt获取;如果折扣不在id.rabatt中,则应从ds.rabatt获取。所以前两个可以是空的,而最后一个总是有折扣 我在设计程序的WHEN部分时遇到了很大的困难。如果您有时间,请看一看并在路上帮助我:Sql server 2008 存储过程中的高级折扣系统,sql-server-2008,stored-procedures,calculated-columns,discount,Sql Server 2008,Stored Procedures,Calculated Columns,Discount,我正在为每月销售制作一个存储过程。在存储过程中,我们有折扣。这个折扣可以从三个不同的表中获得。如果折扣不在id.rabatt中,则应从dp.rabatt获取;如果折扣不在id.rabatt中,则应从ds.rabatt获取。所以前两个可以是空的,而最后一个总是有折扣 我在设计程序的WHEN部分时遇到了很大的困难。如果您有时间,请看一看并在路上帮助我: CASE ( when Isnull(
CASE (
when
Isnull(id.rabatt, Isnull(u.rabatt, id.rabatt)) then..
when
Isnull(dp.rabatt, Isnull(x.rabatt, dp.rabatt)) then..
when
Isnull(ds.rabatt, Isnull(y.rabatt, ds.rabatt)) then..
end)
AS 'Discount',
我必须使用Isnull的原因是,在每个折扣表中,我也有两种不同的折扣,一种是永久的(2999),另一种是有一个选定的期间。就像我在这里展示的:
LEFT OUTER JOIN discount AS id
ON id.identifiers = isa.identifiers
AND id.store = BV.name
AND id.from_date <= isa.sales_date
AND id.to_date >= isa.sales_date
AND id.to_date < '2999-01-01'
LEFT OUTER JOIN discount AS u
ON u.identifiers = isa.identifiers
AND u.to_date = '2999-01-01'
作为id的左侧外部加入折扣
ON id.identifiers=isa.identifiers
并且id.store=BV.name
id.from_date=isa.sales_date
和id.截止日期<'2999-01-01'
左外联接折扣为u
关于u.identifiers=isa.identifiers
和u.to_date='2999-01-01'
另外两张桌子的设计方式相似 使用coalesce函数的方式与使用IsNull函数的方式类似。IsNull和Coalesce之间有一些细微的区别,但对代码有益的显著区别是,您可以拥有多个参数,而无需嵌套它 您的代码:Isnull(id.rabatt,Isnull(u.rabatt,id.rabatt)) 与:合并(id.rabatt,u.rabatt,id.rabatt) 接下来。。。case/when有两种通用形式
Case (Some Condition)
When (Value 1) Then ...
When (Value 2) Then ...
Else (Default Value)
End
或
查看您的代码片段,似乎您正在使用第二个表单,但在when部分中没有比较运算符。我想你想要这样的东西
CASE When Coalesce(id.rabatt, u.rabatt, id.rabatt) Is Not NULL then..
When Coalesce(dp.rabatt, x.rabatt, id.rabatt) Is Not NULL then..
When Coalesce(ds.rabatt, y.rabatt, id.rabatt) Is Not NULL then..
Else (Put a default value here)
end AS [Discount]
我认为您可能不需要一个案例/时间,并且可能只需要一个合并函数即可。将案例/时间替换为:Coalesce(id.rabatt,dp.rabatt,ds.rabatt)作为[折扣]谢谢我明天早上测试并回复你:)工作正常,只需做一些小改动并添加Coalesce。。非常感谢:)
CASE When Coalesce(id.rabatt, u.rabatt, id.rabatt) Is Not NULL then..
When Coalesce(dp.rabatt, x.rabatt, id.rabatt) Is Not NULL then..
When Coalesce(ds.rabatt, y.rabatt, id.rabatt) Is Not NULL then..
Else (Put a default value here)
end AS [Discount]