简体   繁体   English

尝试创建有序SQL表时出错

[英]Error when I try to create ordered SQL table

I'm trying to create a volatile table in SQL with an ORDER BY and I get an error. 我正在尝试使用ORDER BY在SQL中创建易失表,但出现错误。

CREATE VOLATILE TABLE orderd_dates AS
(SELECT * FROM date_table
ORDER BY id_date)
with data primary index (id_date) on commit preserve rows;

The error is: ORDER BY is not allowed in subqueries. 错误是:子查询中不允许ORDER BY。

If I can't use order by, how can I create a volatile table that's ordered? 如果不能使用order by,如何创建已排序的易失表?

SQL tables are inherently unordered. SQL表本质上是无序的。 You need to explicitly use an order by clause when querying the table, not when creating it. 查询表时(而不是创建表时),需要显式使用order by子句。

You could add TOP 100 PERCENT to allow the ORDER BY, but the table would still be unordered, because a table is internally ordered by the Hash of the Primary Index. 您可以添加TOP 100 PERCENT以允许ORDER BY,但是该表仍然是无序的,因为该表是由主索引的哈希在内部进行排序的。 And if you use a NO PRIMARY INDEX TABLE and it would actually be stored in the specified order the optimizer wouldn't know about it. 而且,如果您使用NO PRIMARY INDEX TABLE,并且它实际上将以指定的顺序存储,那么优化器将不会知道它。

The closest thing you can get is to PARTITION BY RANGE_N(id_date BETWEEN DATE '2000-01-01' AND DATE '2050-12-31' EACH INTERVAL '1' DAY : 您可以得到的最接近的信息是PARTITION BY RANGE_N(id_date BETWEEN DATE '2000-01-01' AND DATE '2050-12-31' EACH INTERVAL '1' DAY

CREATE VOLATILE TABLE orderd_dates AS
(SELECT * FROM date_table
)
WITH DATA 
PRIMARY INDEX (id_date)
PARTITION BY Range_N(id_date BETWEEN DATE '2000-01-01'
                                 AND DATE '2050-12-31' EACH INTERVAL '1' DAY)
ON COMMIT PRESERVE ROWS;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM