如何更改SQL查询以在Vertica中工作?

时间:2014-03-21 09:27:31

标签: sql oracle vertica

我们有Oracle 11.2 DB,其中SQL查询:

SELECT STREAM , SUBSTREAM, SEQUENCE_ID + 1 SEQ, FILE_NAME_TIME,TASK_START_TIME, VALIDITY_TOKEN
from time_gap_files s1 
WHERE NOT EXISTS (SELECT NULL from time_gap_files s2 where s2.SEQUENCE_ID=s1.SEQUENCE_ID+
(select DISTINCT FILE_GAP from time_gap_files_rules R, time_gap_files S where
S.STREAM=R.STREAM)) ORDER BY SEQUENCE_ID;

工作正常。

但是当我在Vertica 6中运行相同的查询(具有相同的数据库结构)时 - 我收到了错误消息: 错误2089:不支持子查询表达式中的相关列。

最后一个SQL子查询(select DISTINCT FILE_GAP from time_gap_files_rules R, time_gap_files S where S.STREAM=R.STREAM)应该只返回一个数字,并且可以单独运行。但不是查询中的子查询。

如何更改查询以满足严格的Vertica SQL规则?

表格结构:

time_gap_files                                                        COLUMN_NAME     DATA_TYPE TYPE_NAME COLUMN_SIZE 
--------------- --------- --------- ----------- 
UNIQUE_ID       -5        Integer   19          
STREAM          12        Varchar   20          
SUBSTREAM       12        Varchar   20          
SEQUENCE_ID     2         Numeric   38          
FILE_NAME_TIME  93        Timestamp 26          
CREATION_TIME   93        Timestamp 26          
PROCESSING_TIME 93        Timestamp 26          
FILE_NAME       12        Varchar   100         
RECORD_COUNT    2         Numeric   38          
VALIDITY_TOKEN  -5        Integer   19          
TASK_START_TIME 93        Timestamp 26


time_gap_files_rules
COLUMN_NAME    DATA_TYPE TYPE_NAME COLUMN_SIZE 
-------------- --------- --------- ----------- 
STREAM         12        Varchar   20          
SEQUENCE_RULE  12        Varchar   20          
MIN_SEQ_NUMBER 2         Numeric   38          
MAX_SEQ_NUMBER 2         Numeric   38          
TIME_GAP       2         Numeric   38          
TIME_DEPTH     12        Varchar   20          
FILE_GAP       -5        Integer   19          

2 个答案:

答案 0 :(得分:0)

尝试在您的子查询中明显添加limit-clause:(select DISTINCT FILE_GAP from time_gap_files_rules R, time_gap_files S where S.STREAM=R.STREAM limit 1)

答案 1 :(得分:0)

尝试使用此脚本运行查询:

SELECT 
STREAM , 
SUBSTREAM,
SEQUENCE_ID + 1 SEQ,
FILE_NAME_TIME,
TASK_START_TIME,
VALIDITY_TOKEN
from time_gap_files s1 
WHERE NOT EXISTS (
SELECT NULL from time_gap_files s2
,time_gap_files s3 
where s2.SEQUENCE_ID=(s3.SEQUENCE_ID
+
(
select 
DISTINCT FILE_GAP 
    from time_gap_files_rules R, time_gap_files S where
S.STREAM=R.STREAM
))
) ORDER BY SEQUENCE_ID;

请在此处查看Vertica子查询限制https://my.vertica.com/docs/7.0.x/HTML/index.htm#Authoring/ProgrammersGuide/Queries/SubqueryRestrictions.htm