Netezza中的子查询

时间:2014-01-29 14:45:08

标签: oracle netezza

我正在尝试将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重写此查询?请帮忙。

2 个答案:

答案 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

  • 您无法在SET操作中使用相关子查询(UNIONINTERSECTEXCEPTMINUS)。
  • 您不能在与GROUP BYHAVING子句的聚合中使用相关子查询。
  • 您无法在ORed子句或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')