SQL查询Microsoft Azure Case表达式不适用于新创建的列名/变量

wgmfuz8q  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(89)

我已经用函数创建了新的列名称SKU和Materialnumber。但是当我用这两列创建案例时,系统无法识别这两个对象,我收到一条错误消息:
消息207,级别16,状态1,第8行
列名"材料编号"无效。
消息207,级别16,状态1,第8行
列名"SKU"无效
这是我的准则。

SELECT
     (Dokument +'.'+ Vs) as JBR_with_version,
     Beschreibung as Description_JBR,
     ObjKey,
     SKU=SUBSTRING(ObjKey,7,5)+'-'+SUBSTRING(ObjKey,12,5)+'-'+SUBSTRING(ObjKey,17,2),
     Materialnumber=SUBSTRING(Beschreibung,1,5)+'-'+SUBSTRING(Beschreibung,7,5)+ 
     '-'+SUBSTRING(Beschreibung,13,2),
        CASE
            WHEN Materialnumber != SKU AND Materialnumber like '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]' THEN Materialnumber
            ELSE 'n.a.'
            END AS Materialnumber
FROM
    dbo.SAP_JBR_SKU_Report
WHERE
     ObjKey like '[0-9]%'

如何正确定义物料编号和SKU以使用Case功能?

92dk7w1h

92dk7w1h1#

对于这些“计算”列,应使用SELECT AS

SELECT
     (Dokument +'.'+ Vs) as JBR_with_version,
     Beschreibung as Description_JBR,
     ObjKey,
     (SUBSTRING(ObjKey,7,5)+'-'+SUBSTRING(ObjKey,12,5)+'-'+SUBSTRING(ObjKey,17,2)) AS 'SKU',
     (SUBSTRING(Beschreibung,1,5)+'-'+SUBSTRING(Beschreibung,7,5)+ 
     '-'+SUBSTRING(Beschreibung,13,2)) AS 'Materialnumber',
        CASE
            WHEN Materialnumber != SKU AND Materialnumber like '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]' THEN Materialnumber
            ELSE 'n.a.'
            END AS Materialnumber
FROM
    dbo.SAP_JBR_SKU_Report
WHERE
     ObjKey like '[0-9]%'

相关问题