【问题标题】:AWS Athena (Presto) - multiple WITH statementsAWS Athena (Presto) - 多个 WITH 语句
【发布时间】:2022-01-16 08:11:12
【问题描述】:

有没有办法在 Athena/Presto 中使用多个 WITH 语句?

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";

错误

只允许一个 sql 语句。得到: 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(从“revenue”中选择“cik”,其中“ratio”> = 1.5 AND“year”> = 2016 GROUP BY“cik”有计数(“ratio”)> = 3 ORDER BY“cik”)选择*来自“cik_with_continuous_growth "; #WHERE "收入"."cik" = "cik_with_continuous_growth"."cik";

【问题讨论】:

    标签: sql amazon-athena presto with-statement


    【解决方案1】:
    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";
    

    【讨论】:

      【解决方案2】:

      你试过with a as ( ) , b as () select * from a,b吗?

      【讨论】:

        猜你喜欢
        • 2018-12-20
        • 2021-11-27
        • 2020-12-16
        • 2020-01-13
        • 2021-02-02
        • 2020-02-29
        • 2020-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多