Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad indention after CTE #202

Open
kbrannen opened this issue Jul 20, 2020 · 0 comments
Open

bad indention after CTE #202

kbrannen opened this issue Jul 20, 2020 · 0 comments

Comments

@kbrannen
Copy link

kbrannen commented Jul 20, 2020

I think this may be similar to #192. Of course, one can always argue about what is the correct style. :) This was done on the website (v4.3), default options (only first box checked).

WITH op AS (
    SELECT
        extract(year FROM created_date) AS year,
        extract(month FROM created_date) AS month,
        count(ticket) AS num
    FROM
        tickets
    WHERE
        product = 'x'
    GROUP BY
        year,
        month
),
cl AS (
    SELECT
        extract(year FROM closed_date) AS year,
        extract(month FROM closed_date) AS month,
        count(ticket) AS num
    FROM
        tickets
    WHERE
        product = 'x'
        AND closed_date IS NOT NULL
    GROUP BY
        year,
        month
),
tot AS (
    SELECT
        op.year,
        op.month,
        op.num AS opened,
        coalesce(cl.num, 0) AS closed,
        op.num - coalesce(cl.num, 0) AS diff
    FROM
        op
        LEFT JOIN cl ON (op.year = cl.year
                AND op.month = cl.month))
-- why is this SELECT indented? shouldn't it be on the same level as the WITH?
    SELECT
        year,
        month,
        opened,
        closed,
        diff,
        sum(diff) OVER (ORDER BY year, month) AS total
        FROM
            tot
        ORDER BY
            year,
            month;

So this may or may not be a bug, but it seems to me it should look like (at a high level):

WITH op AS (
    ...
),
cl AS (
    ...
),
tot AS (
    ...
)  -- error starts here, if this had gone back to col 1 like the others, then the select would have been in col 1 too (i think)
SELECT ...
    FROM tot
    ORDER BY year, month
;

So it seems like the last close paren of the last CTE clause is where the problem is. More experimenting shows that it's good 1 or 2 CTE clauses (with op or op+cl above), it's when i add the 3rd (tot) that things go wrong. I can add a 4th and get the same error, but it doesn't get worse, if that helps.

Here's what it looks like with 2 windows, which I believe is correct:

WITH op AS (
    SELECT
        extract(year FROM created_date) AS year,
        extract(month FROM created_date) AS month,
        count(ticket) AS num
    FROM
        tickets
    WHERE
        product = 'x'
    GROUP BY
        year,
        month
),
cl AS (
    SELECT
        extract(year FROM closed_date) AS year,
        extract(month FROM closed_date) AS month,
        count(ticket) AS num
    FROM
        tickets
    WHERE
        product = 'x'
        AND closed_date IS NOT NULL
    GROUP BY
        year,
        month
)
SELECT
    year,
    month,
    opened,
    closed,
    diff,
    sum(diff) OVER (ORDER BY year, month) AS total
FROM
    tot
ORDER BY
    year,
    month

 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant