选择具有连接条件错误的查询

时间:2014-01-12 19:23:58

标签: mysql sql sql-server select hana

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

 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。我找不到任何遗漏的东西。如果有人能提出什么会出现问题?

由于

2 个答案:

答案 0 :(得分: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是如何相关的。根据我发现的here参考手册,左右外连接总是需要连接标准。

另外,我会犹豫使用没有Else语句的Case语句,因为如果大小写不正确则返回NULL。如果这就是你想要的,那很好,但我怀疑你会从替代编程中受益,而不是完全抛弃Else声明。

答案 1 :(得分: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函数,并在返回值为null时提供替换值。

像这样......

 SELECT T0."REFID"
       , T0."REFID_NR"
       , T0."ID_POS"
       , IFNULL(T4."REFID",'Sub_Value') AS "Result"