使用 'with' 个查询快速创建 table

Presto create table with 'with' queries

通常在 Presto 中创建一个 table(从现有数据库 tables),我这样做:

create table abc as (
select...
)

但是为了使我的代码简单,我分解了这样的子查询:

with sub1 as (
select...
),

sub2 as (
select...
),

sub3 as (
select...
)

select
from sub1 join sub2 on ...
          join sub3 on ...

在哪里放置 create table 语句?实际查询比上面的更复杂,所以我尽量避免将子查询放在主查询中。

我相信您需要像这样 'wrap' 整个查询:

create table EXAMPLE as (
with sub1 as (
select ...
),
.......

select 
from sub1....

)

这对于不确定 CREATE 的 INSERT INTO 是可能的 TABLE:

INSERT INTO s1 WITH q1 AS (...) SELECT * FROM q1

也许你可以试一试:

CREATE TABLE s1 as WITH q1 AS (...) SELECT * FROM q1

语法就像在 select 前面加上 create table .. as 一样。例如。以下在 Presto 0.170 上对我有用:

create table memory.default.a as
with w as (
    select * from (values 1) t(x)
)
select * from w;

(我使用实验性的 memory 连接器,因此可以复制粘贴来试用。)

如果涉及字符串,则以下工作

WITH sample AS (
    SELECT * FROM (VALUES ('strA', 'strB'), ('strC', 'strD'), ('strE', 'strF')) AS account (name, cat)
)

SELECT name, cat from sample;

如果整数仅与值有关,则以下工作:-

WITH  slab (SNo,Amount) AS (VALUES (1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(6,6000),(7,7000),(8,8000),(9,9000),(10,10000),(11, 11000),(12,12000),(13,13000),(14,14000),(15,15000),(16,16000),(17,17000),(18,18000),(19,19000),(20,20000),(21,21000),(22,22000),(23,23000),(24,24000),(25,25000),(26,26000),(27,27000),(28,28000),(29,29000),(30,30000),(31,31000),(32,32000),(33,33000),(34,34000),(35,35000),(36,36000),(37,37000),(38,38000),(39,39000),(40,40000),(41,41000),(42,42000),(43,43000),(44,44000),(45,45000),(46,46000),(47,47000),(48,48000),(49,49000),(50,50000),(51,51000)
) 
SELECT * FROM slab;