0

Estou utilizando o warehouse snowflake, tenho que fazer alguns selects

1° Select

select
    WAREHOUSE_NAME,
    TOTAL_ELAPSED_TIME,
    QUERY_TEXT,
    START_TIME
from
    SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
where
    TOTAL_ELAPSED_TIME in (
        select
            max(TOTAL_ELAPSED_TIME)
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            START_TIME BETWEEN $ start_time
            and dateadd(days, 1, $ start_time)
            and WAREHOUSE_ID is not NULL
        group by
            WAREHOUSE_ID
    )
order by
    WAREHOUSE_ID;

2° Select

select
    max (NULLIF((QUEUED_OVERLOAD_TIME), 0)) MAX_Q_O_TIME,
    avg (NULLIF((QUEUED_OVERLOAD_TIME), 0)) AVG_Q_O_TIME,
    count (NULLIF((QUEUED_OVERLOAD_TIME), 0)) RECORDS_Q_O_TIME
from
    SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
where
    START_TIME between $start_time
    and dateadd(days, 1, $start_time)
    and WAREHOUSE_ID is not NULL
group by
    WAREHOUSE_ID
order by
    WAREHOUSE_ID;

3° Select

select
    count(*) RECORDS_T_E_T,
    avg (TOTAL_ELAPSED_TIME) AVG_T_E_T
from
    SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
where
    START_TIME between $ start_time
    and dateadd(days, 1, $ start_time)
    and WAREHOUSE_ID is not NULL
group by
    WAREHOUSE_ID
order by
    WAREHOUSE_ID;

Exemplo de como retorna o primeiro select :

WAREHOUSE_NAME | TOTAL_ELAPSED_TIME | QUERY_TEXT | START_TIME
   WAREHOUSE_1 |               1000 |   EXEMPLE1 | 2019-10-29 01:03:25.548 -0700
   WAREHOUSE_2 |               2000 |   EXEMPLE2 | 2019-10-29 01:00:25.548 -0700

Exemplo de como retorna o segundo select :

MAX_Q_O_TIME | AVG_Q_O_TIME | RECORDS_Q_O_TIME
        1000 |         2000 |               10
        NULL |         NULL |                0

Exemplo de como retorna o terceiro select :

RECORDS_T_E_T | AVG_T_E_T
          100 |       200
           50 |       300

São só exemplos, não são esses valores mesmo

Gostaria de unir tudo isso em um só

WAREHOUSE_NAME | TOTAL_ELAPSED_TIME | QUERY_TEXT | START_TIME |  MAX_Q_O_TIME | AVG_Q_O_TIME | RECORDS_Q_O_TIME | RECORDS_T_E_T | AVG_T_E_T
   WAREHOUSE_1 |               1000 |   EXEMPLE1 | 2019-10-29 |          1000 |         2000 |               10 |          100 |        200
   WAREHOUSE_2 |               2000 |   EXEMPLE2 | 2019-10-29 |          NULL |         NULL |                0 |           50 |        300

Renomeei algumas coisas e no ultimo start time cortei as horas&minutos&segundos para não ficar tão grande o exemplo

Tentei fazer algo como

select
    a.*,
    b.*,
    c.*
from
    (
        select
            WAREHOUSE_NAME,
            TOTAL_ELAPSED_TIME,
            QUERY_TEXT,
            START_TIME,
            WAREHOUSE_ID
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            TOTAL_ELAPSED_TIME in (
                select
                    max(TOTAL_ELAPSED_TIME)
                from
                    SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
                where
                    START_TIME between $start_time
                    and dateadd(days, 1, $start_time)
                    and WAREHOUSE_ID is not NULL
             group by
            WAREHOUSE_ID
            )
        order by
            WAREHOUSE_ID
    ) as a,
    (
        select
            max (NULLIF((QUEUED_OVERLOAD_TIME), 0)),
            avg (NULLIF((QUEUED_OVERLOAD_TIME), 0)),
            count (NULLIF((QUEUED_OVERLOAD_TIME), 0))
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            START_TIME between $start_time
            and dateadd(days, 1, $start_time)
            and WAREHOUSE_ID is not NULL
        group by
            WAREHOUSE_ID
        order by
            WAREHOUSE_ID
    ) as b,
    (
        select
            count(*) RECORDS_T_E_T,
            avg (TOTAL_ELAPSED_TIME) AVG_T_E_T
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            START_TIME between $start_time
            and dateadd(days, 1, $start_time)
            and WAREHOUSE_ID is not NULL
        group by
            WAREHOUSE_ID
        order by
            WAREHOUSE_ID
    ) as c;

Mas está retornando 64 rows aqui ( acho que por que são 4 warehouses ), enfim, já tentei usando join, também, mesmo problema, existe algum comando que ainda não conheço?

Se tento utilizar UNION, algo como :

select
    a.*
from
    (
        select
            WAREHOUSE_NAME,
            TOTAL_ELAPSED_TIME,
            QUERY_TEXT,
            START_TIME,
            WAREHOUSE_ID
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            TOTAL_ELAPSED_TIME in (
                select
                    max(TOTAL_ELAPSED_TIME)
                from
                    SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
                where
                    START_TIME between $start_time
                    and dateadd(days, 1, $start_time)
                    and WAREHOUSE_ID is not NULL
            )
        group by
            WAREHOUSE_ID
        UNION
        select
            max (NULLIF((QUEUED_OVERLOAD_TIME), 0)),
            avg (NULLIF((QUEUED_OVERLOAD_TIME), 0)),
            count (NULLIF((QUEUED_OVERLOAD_TIME), 0))
        from
            SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
        where
            START_TIME between $start_time
            and dateadd(days, 1, $start_time)
            and WAREHOUSE_ID is not NULL
        group by
            WAREHOUSE_ID
    ) a
order by
    WAREHOUSE_ID;

Recebo o erro :

SQL compilation error: invalid number of result columns for set operator input branches, expected 5, got 3 in branch {2}

Não tenho os mesmos tipos de dados nos dois selects, não posso usar UNION.

6
  • 1
    Possível duplicata de SQL ajuda a unir 2 selects
    – rbz
    Commented 2/12/2019 às 17:07
  • Não, mesmo tentando assim, só retorna erro, talvez seja pq o snowflake não aceite alguma dessas funções, em geral ele só aceita coisas do mysql. Commented 2/12/2019 às 17:45
  • Até onde sei o snowflake não aceita essa função, tenta usar MySQL Workbench é mais provável que consiga lá pela variedade de recursos
    – Dakota
    Commented 2/12/2019 às 17:57
  • 1
    Seu union está errado. Entenda o conceito antes de aplicá-lo. Aposto que irá conseguir!
    – rbz
    Commented 2/12/2019 às 18:17
  • Estou tentando a 3 dias, usei tudo que eu podia, de várias formas, não consegui, por isso vim aqui perguntar, o exemplo que vc deu não é equivalente, tentei aplicar ele, da o mesmo problema de qualquer forma. Commented 2/12/2019 às 18:24

1 Resposta 1

3

Você já tentou usar WITH QUERIES para unir esses dados?

Ficaria assim:

WITH 
query1 AS 
(
   select
       WAREHOUSE_ID,
       WAREHOUSE_NAME,
       TOTAL_ELAPSED_TIME,
       QUERY_TEXT,
       START_TIME
   from
       SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
   where
       TOTAL_ELAPSED_TIME in (
           select
               max(TOTAL_ELAPSED_TIME)
           from
               SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
           where
               START_TIME BETWEEN $ start_time
               and dateadd(days, 1, $ start_time)
               and WAREHOUSE_ID is not NULL
           group by
               WAREHOUSE_ID
       )
),
query2 AS 
(    
   select
       WAREHOUSE_ID,
       max (NULLIF((QUEUED_OVERLOAD_TIME), 0)) MAX_Q_O_TIME,
       avg (NULLIF((QUEUED_OVERLOAD_TIME), 0)) AVG_Q_O_TIME,
       count (NULLIF((QUEUED_OVERLOAD_TIME), 0)) RECORDS_Q_O_TIME
   from
       SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
   where
       START_TIME between $start_time
       and dateadd(days, 1, $start_time)
       and WAREHOUSE_ID is not NULL
   group by
       WAREHOUSE_ID
),
query3 AS 
(  
   select
       WAREHOUSE_ID,
       count(*) RECORDS_T_E_T,
       avg (TOTAL_ELAPSED_TIME) AVG_T_E_T
   from
       SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
   where
       START_TIME between $ start_time
       and dateadd(days, 1, $ start_time)
       and WAREHOUSE_ID is not NULL
   group by
       WAREHOUSE_ID
)
SELECT 
   query1.WAREHOUSE_NAME,
   query1.TOTAL_ELAPSED_TIME,
   query1.QUERY_TEXT,
   query1.START_TIME,
   query2.MAX_Q_O_TIME,
   query2.AVG_Q_O_TIME,
   query2.RECORDS_Q_O_TIME,  
   query3.RECORDS_T_E_T,
   query3.AVG_T_E_T
FROM query1
INNER JOIN query2 ON query2.WAREHOUSE_ID = query1.WAREHOUSE_ID
INNER JOIN query3 ON query3.WAREHOUSE_ID = query1.WAREHOUSE_ID
ORDER BY 
       queyr1.WAREHOUSE_ID
2
  • Não, nem sabia que era possível, vou estudar sobre, mas o resultado retornou certo, obrigado! Só uma observação, vc escreveu 'queyr' no select ao invés de 'query'. Commented 2/12/2019 às 19:58
  • Realmente estava errado, obrigado por avisar. Já está corrigido. Commented 2/12/2019 às 20:01

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .