Presto使用'with'查询创建表

时间:2017-03-02 18:36:46

标签: sql presto

通常在Presto中创建一个表(来自现有的db表),我这样做:

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语句?实际查询比上面的更复杂,所以我试图避免将子查询放在主查询中。

4 个答案:

答案 0 :(得分:4)

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

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

也许你可以试一试:

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

答案 1 :(得分:1)

语法就像你在create table .. as前加上select一样。例如。以下在Presto 0.170上为我工作:

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

(我使用实验性的memory连接器,这样就可以通过复制进行试用了。)

答案 2 :(得分:0)

我相信你需要像这样“包装”整个查询:

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

select 
from sub1....

)

答案 3 :(得分:0)

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

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;