2 years ago

#69614

test-img

Andreas L.

Multiple SQL statements connected via WITH fail due to 'mismatched input'

The following SQL - statement arose due to the fact that SQL cannot process in the same query newly created columns and calculations with these.

The error message is:

line 22:1: mismatched input 'WITH'. Expecting: '(', 'SELECT', 'TABLE', 'VALUES'

The SQL-Statement:

WITH cte AS (
    SELECT
    date_add('day',
    CAST(
    split_part(CAST(_calendar_key AS VARCHAR),
    '|',1)
    AS bigint),
    CAST('1899-12-30' AS DATE)
    ) AS date,
    date_add('day',
    CAST(
    split_part(CAST(_calendar_key AS VARCHAR),
    '|',2) AS bigint),
    CAST('1899-12-30' AS DATE)
    ) AS end_date,
    date_diff('day', date, CURRENT_DATE) AS days_since_cycle_start,
    _level_team,
    bu_key_product
    FROM f_target
)

WITH cte2 AS (
    SELECT
    date,
    end_date,
    days_since_cycle_start,
    _level_team,
    bu_key_product,
    end_date - date AS cycle_plan_days
    FROM cte
)

WITH cte3 AS (
    SELECT
    date,
    end_date,
    days_since_cycle_start,
    _level_team,
    bu_key_product,
    cycle_plan_days
    days_since_cycle_start / cycle_plan_days AS percentage_elapsed
    FROM cte2
)

SELECT * FROM cte3
WHERE _level_team = 1 AND
bu_key_product = 'some_random_string' AND
date < CURRENT_DATE AND end_date > CURRENT_DATE

mysql

sql

presto

amazon-athena

0 Answers

Your Answer

Accepted video resources