1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Union partitioned table and view, then query using _PARTITIONTIME not _pt

Discussão em 'Outras Linguagens' iniciado por Stack, Maio 18, 2021.

  1. Stack

    Stack Membro Participativo

    I have a partitioned table called table1 and I need to query it as below:

    SELECT * FROM table1
    WHERE DATE(_PARTITIONTIME) = "2021-04-20"
    AND id>50


    Then I realised there are some missing data in table1 and I have to union with table2 (which is a view) to have complete data so :

    WITH
    unioned AS(
    SELECT
    id, type, _partitiontime
    FROM
    `table1`
    WHERE
    _PARTITIONTIME > "2021-04-17"
    UNION ALL
    SELECT
    id, type, _pt as _partitiontime,
    FROM
    `table2`
    WHERE
    _pt > "2021-04-17")

    SELECT * FROM unioned
    WHERE _partitiontime = "2021-04-20"
    AND id>50


    so I get Invalid field name "_partitiontime". error. If I write my query as below then it will work:

    WITH
    unioned AS(
    SELECT
    id, type, _partitiontime as _pt
    FROM
    `table1`
    WHERE
    _PARTITIONTIME > "2021-04-17"
    UNION ALL
    SELECT
    id, type, _pt,
    FROM
    `table2`
    WHERE
    _pt > "2021-04-17")

    SELECT * FROM unioned
    WHERE _pt = "2021-04-20"
    AND id>50


    but because there are lots of nested queries which are using the above query so I prefer to query the unioned table using _partitiontime not _pt otherwise I have to change lots of codes. Is there any way that the below query works:

    SELECT * FROM unioned
    WHERE _partitiontime = "2021-04-20"
    AND id>50

    Continue reading...

Compartilhe esta Página