0

我有一个选择查询来获取结果集,我的查询如下:

 SELECT T0."REFID", T0."REFID_NR", T0."ID_POS", 
 CASE 
    WHEN T4."REFID" IS NOT NULL 
    THEN T4."REFID"
 END AS "result"
FROM "SYSTEM"."T00_SOFT" T0
LEFT OUTER JOIN 
(
SELECT T1."REFID", T1."REFID_NR", T1."ID_POS",T1."ARTIKEL" ,T2."VEHW", T2."VNHW" , 
(((T2."VEHW"*100)/(T2."VNHW"))*((T1."ER_AW"*100)))/(T1."BAS_AW") AS "claims"
FROM "SYSTEM"."T00_SOFT" T1
INNER JOIN "SYSTEM"."T00_EG" T2
ON T1."REFID" = T2."REFID"
AND T1."ORGID_WE" = T2."ORGID"
AND T1."ARTIKEL" = T2."MATNR" ) T4;

当我执行查询时,我收到以下错误:

Could not execute 'SELECT T0."REFID", T0."REFID_NR", T0."ID_POS", CASE WHEN T4."REFID" IS NOT NULL THEN ...' in 42 ms 382 µs . 
SAP DBTech JDBC: [257] (at 521): sql syntax error: line 15 col 33 (at pos 521)

这里第 15 行是指最后一行,表示错误存在于T4. 我没有发现任何遗漏。如果有人可以提出什么问题?

谢谢

4

2 回答 2

2

您在 T0 和 T4 之间的联接中缺少 ON 条件。我会将您的查询改写为:

SELECT T0."REFID", T0."REFID_NR", T0."ID_POS", 
 CASE 
    WHEN T4."REFID" IS NOT NULL 
    THEN T4."REFID"
 END AS "result"
FROM "SYSTEM"."T00_SOFT" T0
LEFT OUTER JOIN 
    (
    SELECT T1."REFID", T1."REFID_NR", T1."ID_POS",T1."ARTIKEL" ,T2."VEHW", T2."VNHW" , 
    (((T2."VEHW"*100)/(T2."VNHW"))*((T1."ER_AW"*100)))/(T1."BAS_AW") AS "claims"
    FROM "SYSTEM"."T00_SOFT" T1
        INNER JOIN "SYSTEM"."T00_EG" T2
            ON T1."REFID" = T2."REFID"
                AND T1."ORGID_WE" = T2."ORGID"
                AND T1."ARTIKEL" = T2."MATNR" ) T4
    ON T0."REFID" = T4."REFID"

您加入了 T0 和 T4,但没有描述它们之间的关系。请记住,仅仅因为您在 From 子句的子查询中定义了 TOO_SOFT 和 TOO_EG 如何相关,并不意味着 T0 和 T4 是如何相关的。根据我在这里找到的参考手册,左外连接和右外连接始终需要连接标准。

另外,我会犹豫是否使用没有 Else 语句的 Case 语句,因为如果 case 不正确,那么它将返回 NULL。如果这就是您想要的,那太好了,但我怀疑您将从替代方案中的编程中受益,而不是完全忽略 Else 语句。

于 2014-01-13T20:03:41.317 回答
0
 SELECT T0."REFID", T0."REFID_NR", T0."ID_POS", 
 CASE 
    WHEN T4."REFID" IS NOT NULL 
    THEN T4."REFID"       ---<-- T4 is a Table Alias you need to mention 
 END AS "result"               -- the column name as well like T4.ColumnName

如果您只检查列中可能的空值,则可以使用 ISNULL 函数并在返回值为空时提供替代值。

像这样的东西......

 SELECT T0."REFID"
       , T0."REFID_NR"
       , T0."ID_POS"
       , IFNULL(T4."REFID",'Sub_Value') AS "Result"
于 2014-01-12T19:28:34.080 回答