我正在尝试将SQL 11从Oracle 11 G移植到Netezza。
以下是我在Oracle中的查询:
SELECT
ID, TEACHERID, CLASS, SECTION, MAJOR, SUPERVISOR, COURSE, SCORE, SCOREDATE,
(select SCORE from STUDENT_SCORES d2 where d2.ID=d1.ID and d2.SCOREDATE (d1.SCOREDATE)-28 and d1.COURSE=d2.COURSE) as PRIORSCORE,
(d1.SCOREDATE)-28 as PRIORSCOREDT,
REMCD
FROM
STUDENT_SCORES d1
WHERE
TEACHERID='T1' AND SCOREDATE=(SELECT MAX(SCOREDATE) FROM STUDENT_SCORES WHERE TEACHERID='T1')
当我在Netezza中运行时,我收到一个错误:
ERROR: (2) This form of correlated query is not supported - consider rewriting
如何为Netezza重写此查询?请帮忙。
答案 0 :(得分:2)
我建议将代码重写为:
select d1.ID, d1.TEACHERID, d1.CLASS, d1.SECTION, d1.MAJOR, d1.SUPERVISOR, d1.COURSE, d1.SCORE, d1.SCOREDATE
,d2.SCORE as PRIORSCORE
,d2.SCOREDATE as PRIORSCOREDT
,d1.REMCD
from STUDENT_SCORES d1 left outer join
(select * from STUDENT_SCORES ss where ss.SCOREDATE = (SELECT MAX(SCOREDATE)-28 FROM STUDENT_SCORES WHERE TEACHERID='T1') d2
on d1.ID=d2.ID
and d1.COURSE=d2.COURSE
where d1.TEACHERID='T1'
and d1.SCOREDATE = (SELECT MAX(SCOREDATE) FROM STUDENT_SCORES WHERE TEACHERID='T1')
答案 1 :(得分:1)
根据Netezza Docs
UNION
,INTERSECT
,EXCEPT
和MINUS
)。GROUP BY
和HAVING
子句的聚合中使用相关子查询。CASE
/ WHEN
表达式中使用相关子查询。IN
列表中使用相关子查询。SELECT
列表中使用相关子查询。重写查询:( Oracle)(不确定 Netezza 语法)
SELECT
d1.ID, d1.TEACHERID, d1.CLASS, d1.SECTION, d1.MAJOR, d1.SUPERVISOR, d1.COURSE, d1.SCORE, d1.SCOREDATE,
d2.SCORE as PRIORSCORE,
(d1.SCOREDATE)-28 as PRIORSCOREDT,
d1.REMCD
FROM
STUDENT_SCORES d1
LEFT JOIN STUDENT_SCORES d2
ON (d2.ID=d1.ID and d2.SCOREDATE = (d1.SCOREDATE)-28 and d1.COURSE=d2.COURSE)
WHERE
d1.TEACHERID='T1' AND d1.SCOREDATE=(SELECT MAX(d3.SCOREDATE) FROM STUDENT_SCORES d3 WHERE d3.TEACHERID='T1')