Vertica SQL - Agg-функция, содержащая подзапросы - PullRequest
0 голосов
/ 17 мая 2018

Ниже запрос возвращает ошибку: агрегированный вызов функции не может содержать подзапросов.

Я хочу динамически получать данные о прошлом месяце, предшествующем прошлом месяце и месяце прошлого года, чтобы использовать их для расчета метрики.

select      pa14.Group_nm AS Group_nm,
                pa14.Sub_Group_nm AS Sub_Group_nm,
                pa14.metric1 AS metric1,
                pa14.metric2 AS metric2,
                pa14.metric3 AS metric3,
                pa14.metric4 AS metric4,
                pa14.metric5 AS metric5
from         (select     a11.sub_grp_nm AS Sub_Group_nm,
                                a11.grp_nm AS Group_nm,
                                sum(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 100) from  summary_table ) THEN a11.scr_val ELSE NULL END) AS metric1,
                                max(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 100) from  summary_table ) THEN 1 ELSE 0 END) AS flag1,
                                sum(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 2) from  summary_table ) THEN a11.scr_val ELSE NULL END) AS metric2,
                                max(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 2) from  summary_table ) THEN 1 ELSE 0 END) AS flag2,
                                sum(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 1) from  summary_table ) THEN a11.scr_val ELSE NULL END) AS metric3,
                                max(CASE WHEN a11.mnth_id in (select (max(mnth_id) - 1) from  summary_table ) THEN 1 ELSE 0 END) AS flag3,
                                sum(a11.mnth_over_mnth_scr_val) AS metric4,
                                sum(a11.yr_over_yr_scr_val) AS metric5
                from         summary_table           a11
                group by  a11.sub_grp_nm,
                                a11.grp_nm
                )               pa14
where      (pa14.flag1 = 1
and pa14.flag2 = 1
and pa14.flag3 = 1)

Я читал другие посты об этой проблеме, но не смог найти решение для моего конкретного случая использования. Надеюсь, ты сможешь мне помочь.

1 Ответ

0 голосов
/ 17 мая 2018

В вашем внутреннем подзапросе вы используете вложенный подзапрос с агрегированным результатом для возврата значений в предложение IN ..
вам следует избегать этого, используя соединение

 select     a11.sub_grp_nm AS Sub_Group_nm,
            a11.grp_nm AS Group_nm,
            sum(CASE WHEN a11.mnth_id =  max_mnth - 100 THEN a11.scr_val ELSE NULL END) AS metric1,
            max(CASE WHEN a11.mnth_id =  max_mnth - 100 THEN 1 ELSE 0 END) AS flag1,
            sum(CASE WHEN a11.mnth_id =  max_mnth - 2  THEN a11.scr_val ELSE NULL END) AS metric2,
            max(CASE WHEN a11.mnth_id =  max_mnth - 2  THEN 1 ELSE 0 END) AS flag2,
            sum(CASE WHEN a11.mnth_id =  max_mnth - 1  THEN a11.scr_val ELSE NULL END) AS metric3,
            max(CASE WHEN a11.mnth_id =  max_mnth - 1  THEN 1 ELSE 0 END) AS flag3,
            sum(a11.mnth_over_mnth_scr_val) AS metric4,
            sum(a11.yr_over_yr_scr_val) AS metric5
from         summary_table           a11 
INNER JOIN ( 
  select summary_table.sub_grp_nm, summary_table.grp_nm, max(mnth_id)  max_mnth
  from summary_table 
  group by summary_table.sub_grp_nm, summary_table.grp_nm 
) T ON T.sub_grp_nm = a11.sub_grp_nm, T.grp_nm = a11.grp_nm

например:

select      pa14.Group_nm AS Group_nm,
                pa14.Sub_Group_nm AS Sub_Group_nm,
                pa14.metric1 AS metric1,
                pa14.metric2 AS metric2,
                pa14.metric3 AS metric3,
                pa14.metric4 AS metric4,
                pa14.metric5 AS metric5
from         (
     select     a11.sub_grp_nm AS Sub_Group_nm,
                a11.grp_nm AS Group_nm,
                sum(CASE WHEN a11.mnth_id =  max_mnth - 100 THEN a11.scr_val ELSE NULL END) AS metric1,
                max(CASE WHEN a11.mnth_id =  max_mnth - 100 THEN 1 ELSE 0 END) AS flag1,
                sum(CASE WHEN a11.mnth_id =  max_mnth - 2  THEN a11.scr_val ELSE NULL END) AS metric2,
                max(CASE WHEN a11.mnth_id =  max_mnth - 2  THEN 1 ELSE 0 END) AS flag2,
                sum(CASE WHEN a11.mnth_id =  max_mnth - 1  THEN a11.scr_val ELSE NULL END) AS metric3,
                max(CASE WHEN a11.mnth_id =  max_mnth - 1  THEN 1 ELSE 0 END) AS flag3,
                sum(a11.mnth_over_mnth_scr_val) AS metric4,
                sum(a11.yr_over_yr_scr_val) AS metric5
    from         summary_table           a11 
    INNER JOIN ( 
      select summary_table.sub_grp_nm, summary_table.grp_nm, max(mnth_id)  max_mnth
      from summary_table 
      group by summary_table.sub_grp_nm, summary_table.grp_nm 
    ) T ON T.sub_grp_nm = a11.sub_grp_nm, T.grp_nm = a11.grp_nm 
)               pa14
where      (pa14.flag1 = 1
and pa14.flag2 = 1
and pa14.flag3 = 1)
...