AWS Athena (Presto) - multiple WITH statements
Asked Answered
H

2

6

Is there a way to use multiple WITH statements in Athena/Presto?

WITH "revenue" AS (
    SELECT 
        "cik",
        "accession",
        "year",
        "quarter",
        "form type" as "form_type",
        CAST("value" AS bigint) as "revenue",
        CAST("value" - lag("value") over (partition by "cik") AS bigint) as "increment",
        ROUND("value" / lag("value") over (partition by "cik"),2) as "ratio"
    FROM "gaap" 
    WHERE 
        "form type" IN ('10-K')
        AND "rep" = 'revenue'
    ORDER BY
        "cik", "year", "quarter", "fs" desc
)

WITH "cik_with_continuous_growth" AS (
    SELECT "cik"
    FROM "revenue"
    WHERE
        "ratio" >= 1.5
        AND "year" >= 2016
    GROUP BY "cik"
    HAVING COUNT("ratio") >= 3
    ORDER BY "cik"
)

SELECT * FROM "cik_with_continuous_growth";

Error

Only one sql statement is allowed. Got: WITH "revenue" AS ( SELECT "cik", "accession", "year", "quarter", "form type" as "form_type", CAST("value" AS bigint) as "revenue", CAST("value" - lag("value") over (partition by "cik") AS bigint) as "increment", ROUND("value" / lag("value") over (partition by "cik"),2) as "ratio" FROM "gaap" WHERE "form type" IN ('10-K') AND "rep" = 'revenue' ORDER BY "cik", "year", "quarter", "fs" desc ) WITH "cik_with_continuous_growth" AS ( SELECT "cik" FROM "revenue" WHERE "ratio" >= 1.5 AND "year" >= 2016 GROUP BY "cik" HAVING COUNT("ratio") >= 3 ORDER BY "cik" ) SELECT * FROM "cik_with_continuous_growth"; #WHERE "revenue"."cik" = "cik_with_continuous_growth"."cik";

Haplosis answered 15/1, 2022 at 23:22 Comment(0)
I
8

have you tried with a as ( ) , b as () select * from a,b ?

Ill answered 15/1, 2022 at 23:30 Comment(0)
N
21
WITH "revenue" AS (,
     SELECT "cik", "accession", year, quarter.
            "form type" as "form_type",
            CAST("value" AS bigint) as "revenue",
            CAST("value" - lag("value") over (partition by "cik") AS bigint) as "increment",
            ROUND("value" / lag("value") over (partition by "cik"),2) as "ratio"
     FROM "gaap" 
     WHERE "form type" IN ('10-K') AND
    "rep" = 'revenue' AND
    ORDER BY "cik", "year", "quarter", "fs" desc
),
"cik_with_continuous_growth" AS (
 SELECT "cik"
 FROM "revenue"
 WHERE "ratio" >= 1.5 AND
       "year" >= 2016
 GROUP BY "cik"
 HAVING COUNT("ratio") >= 3
 ORDER BY "cik"
)
SELECT * FROM "cik_with_continuous_growth";
Neuralgia answered 16/1, 2022 at 0:11 Comment(0)
I
8

have you tried with a as ( ) , b as () select * from a,b ?

Ill answered 15/1, 2022 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.