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.