需要帮助以查找查询中的问题并对其进行优化

时间:2018-10-11 17:16:53

标签: sql database db2

DB2中使用以下查询来获取记录。如果未找到任何记录,则尝试在该列中使用默认值。但是事情没有按预期进行。查询肯定存在一些问题,还需要对其进行优化。期待一些帮助/建议/正面批评。

查询:

SELECT DISTINCT(t1.col1),t1.col2,t1.col3,t1.col4,t1.col5
CASE 
WHEN t1.col1 = '' THEN ‘AA99’ ELSE t1.col1 END AS col1
WHEN t1.col2 = '' THEN ‘AA99’ ELSE t1.col2 END AS col2
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3 
INNER JOIN TABLE3 t3
ON (t1.col2 = t3.col2)
INNER JOIN TABLE4 t4
ON (t1.col1 = t4.col1 AND t1.col3 = t4.col3)
WHERE (((t1.col1 <> 'NA' OR t1.col1 IS NOT NULL) AND (t2.col1 <> 'NA' OR t2.col1 IS NOT NULL)) 
AND ((t1.col2 <> 'NA' OR t1.col2 IS NOT NULL) AND (t2.col2 <> 'NA' OR t2.col2 IS NOT NULL))
GROUP BY t1.col1,t1.col2 ;

错误:

1) [Code: -104, SQL State: 42601]  An unexpected token "WHEN" was found following ".col1
CASE 
".  Expected tokens may include:  "INTO".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.22.29
2) [Code: -727, SQL State: 56098]  An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-104", SQLSTATE "42601" and message tokens "WHEN|.col1
CASE 
|INTO".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.22.29
3) Group By is also not working

Update_1:

根据建议尝试以下操作:

SELECT DISTINCT (t1.col1),t1.col2,t1.col3,t1.col4,t1.col5,
CASE 
    WHEN t1.col1 = '' THEN ‘AA99’
    ELSE t1.col1
END AS newcol1,
CASE
    WHEN t1.col2 = '' THEN ‘AA99’
    ELSE t1.col2
END AS newcol2
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3 
INNER JOIN TABLE3 t3
ON (t1.col2 = t3.col2)
INNER JOIN TABLE4 t4
ON (t1.col1 = t4.col1 AND t1.col3 = t4.col3)
WHERE (((t1.col1 <> 'NA' OR t1.col1 IS NOT NULL) AND (t2.col1 <> 'NA' OR t2.col1 IS NOT NULL)) 
AND ((t1.col2 <> 'NA' OR t1.col2 IS NOT NULL) AND (t2.col2 <> 'NA' OR t2.col2 IS NOT NULL))
GROUP BY newcol1,newcol2;   

错误:

1) [Code: -206, SQL State: 42703]  "newcol1" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.22.29
2) [Code: -727, SQL State: 56098]  An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-206", SQLSTATE "42703" and message tokens "newcol1".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.22.29  

这很尴尬,但我在这里做的事情没傻。

2 个答案:

答案 0 :(得分:2)

您缺少第二个case-when语句的CASE部分。同样,您要选择col1和col2,然后使用别名使用case-when语句再次选择它们,这可以按问题进行分组。试试这个版本:

SELECT DISTINCT 
t1.col1,
t1.col2,
t1.col3,
t1.col4,
t1.col5,
CASE 
    WHEN t1.col1 = '' THEN ‘AA99’
    ELSE t1.col1
END AS column1,
CASE
    WHEN t1.col2 = '' THEN ‘AA99’
    ELSE t1.col2
END AS column2
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3 
INNER JOIN TABLE3 t3
ON (t1.col2 = t3.col2)
INNER JOIN TABLE4 t4
ON (t1.col1 = t4.col1 AND t1.col3 = t4.col3)
WHERE (((t1.col1 <> 'NA' OR t1.col1 IS NOT NULL) AND (t2.col1 <> 'NA' OR t2.col1 IS NOT NULL)) 
AND ((t1.col2 <> 'NA' OR t1.col2 IS NOT NULL) AND (t2.col2 <> 'NA' OR t2.col2 IS NOT NULL))
GROUP BY column1,column2 ;

答案 1 :(得分:1)

如果使用IBM Data Studio,它将突出显示语法错误的开始位置。这样可以更轻松地编写语法正确的SQL,例如

SELECT  DISTINCT
        t1.col1,t1.col2,t1.col3,t1.col4,t1.col5
,       CASE WHEN t1.col1 = '' THEN 'AA99' ELSE t1.col1 END AS col1
,       CASE WHEN t1.col2 = '' THEN 'AA99' ELSE t1.col2 END AS col2
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3)
INNER JOIN TABLE3 t3
ON (t1.col2 = t3.col2)
INNER JOIN TABLE4 t4
ON (t1.col1 = t4.col1 AND t1.col3 = t4.col3)
WHERE ((t1.col1 <> 'NA' OR t1.col1 IS NOT NULL) AND (t2.col1 <> 'NA' OR t2.col1 IS NOT NULL)) 
AND   ((t1.col2 <> 'NA' OR t1.col2 IS NOT NULL) AND (t2.col2 <> 'NA' OR t2.col2 IS NOT NULL))
GROUP BY t1.col1,t1.col2 ;