我对 SQL 编码比较陌生,并且有一个关于 case 语句的问题。
我想要实现的目标:我想创建一个用于计算正确的过时规定的查询。
为此,我需要创建一个名为的列Inventory Reach
和一个叫Devaluation Class
。这两个字段都会被计算。
现场贬值类别的结果取决于库存达到计算。 IE。根据库存达到情况确定贬值等级。例如。如果inventory reach
大于 9 ,贬值等级为 1 (100%)。执行下面的代码时,我收到以下错误消息:
列名称“库存范围”无效。
所以对我来说,第二个 case 语句似乎失败了,它与第一个 case 语句挂在一起。但我很难找到问题的答案。如果有人能为黑暗带来一些光明,我将不胜感激。
先感谢您
Select
[Material]
,[Plnt]
,case
when [calculate 5-year demand] = 0
then 9.01
when [BAAS SO GI (601) 36] = 0
then 9.01
when [MS] <> 'BI' or [MS] <> 'BO'
then ([Stock all SP WH]/([calculate 5-year demand]/5))
when [MS] = 'BO'
then ([Stock all SP WH]/[BAAS SO GI (601) 36])
when [MS] ='BI'
then 0
else 9.01
end as [Inventory Reach]
,case
when [Inventory Reach] > 9
then 1
else 0.9
end as [Devaluation Class]
from [BAAS_PowerBI].[dbo].[Obs]
不允许使用列别名FROM
, WHERE
, GROUP BY
, or HAVING
条款。您可以使用子查询或 CTE,但 SQL Server 通过以下方式支持横向联接:APPLY
关键词。这对于引入列别名非常有用:
select o.Material, o.Plnt, v.[Inventory Reach],
(case when v.[Inventory Reach] > 9
then 1
else 0.9
end) as [Devaluation Class]
from [BAAS_PowerBI].[dbo].[Obs] o cross apply
(values (case when o.[calculate 5-year demand] = 0
then 9.01
when o.[BAAS SO GI (601) 36] = 0
then 9.01
when o.[MS] in ('BI', 'BO')
then (o.[Stock all SP WH] / (o.[calculate 5-year demand] / 5))
when o.[MS] = 'BO'
then (o.[Stock all SP WH] / o.[BAAS SO GI (601) 36])
when o.[MS] ='BI'
then 0
else 9.01
end
)
) values([Inventory Reach])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)