Advertisement
Noordle

Untitled

Jan 17th, 2025
38
0
15 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create materialized view public.kpi_active_keywords_mv as
  2. SELECT sub.kpi_id       AS id,
  3.        sub.kpi_id,
  4.        sub.spider_id,
  5.        sub.spidername,
  6.        max(
  7.                CASE
  8.                    WHEN sub.module = 'fp_kpi_keywords'::text THEN sub.keywords
  9.                    ELSE ARRAY []::integer[]
  10.                    END) AS search_first_page,
  11.        max(
  12.                CASE
  13.                    WHEN sub.module = 'top_kpi_keywords'::text THEN sub.keywords
  14.                    ELSE ARRAY []::integer[]
  15.                    END) AS search_top,
  16.        max(
  17.                CASE
  18.                    WHEN sub.module = 'products_fp_kpi_keywords'::text THEN sub.keywords
  19.                    ELSE ARRAY []::integer[]
  20.                    END) AS search_products_on_first_page,
  21.        max(
  22.                CASE
  23.                    WHEN sub.module = 'products_top_kpi_keywords'::text THEN sub.keywords
  24.                    ELSE ARRAY []::integer[]
  25.                    END) AS search_products_on_top
  26. FROM (SELECT kpi.kpi_id,
  27.              s.main_spider_id        AS spider_id,
  28.              s.spidername,
  29.              'fp_kpi_keywords'::text AS module,
  30.              array_agg(kw.id)        AS keywords
  31.       FROM kpi_searchfirstpagekpi_keywords kkw
  32.                JOIN search_analytics_rpckeywordpreference ks ON kkw.rpckeyword_id = ks.keyword_id
  33.                JOIN search_analytics_rpckeyword kw ON kw.id = ks.keyword_id
  34.                JOIN kpi_searchfirstpagekpi kpi ON kkw.searchfirstpagekpi_id = kpi.id
  35.                JOIN (SELECT si.id AS main_spider_id,
  36.                             ss.id AS search_spider_id,
  37.                             si.spidername
  38.                      FROM items_spider si
  39.                               LEFT JOIN items_spider ss ON (si.spidername::text || '_search'::text) = ss.spidername::text AND ss.type = 3
  40.                      WHERE si.type = 1) s ON ks.spider_id = s.search_spider_id
  41.       WHERE kw.removed = false
  42.         AND kw.is_visible = true
  43.       GROUP BY kpi.kpi_id, s.main_spider_id, s.spidername
  44.       UNION
  45.       SELECT kpi.kpi_id,
  46.              s.main_spider_id         AS spider_id,
  47.              s.spidername,
  48.              'top_kpi_keywords'::text AS module,
  49.              array_agg(kw.id)         AS keywords
  50.       FROM kpi_searchtopkpi_keywords kkw
  51.                JOIN search_analytics_rpckeywordpreference ks ON kkw.rpckeyword_id = ks.keyword_id
  52.                JOIN search_analytics_rpckeyword kw ON kw.id = ks.keyword_id
  53.                JOIN kpi_searchtopkpi kpi ON kkw.searchtopkpi_id = kpi.id
  54.                JOIN (SELECT si.id AS main_spider_id,
  55.                             ss.id AS search_spider_id,
  56.                             si.spidername
  57.                      FROM items_spider si
  58.                               LEFT JOIN items_spider ss ON (si.spidername::text || '_search'::text) = ss.spidername::text AND ss.type = 3
  59.                      WHERE si.type = 1) s ON ks.spider_id = s.search_spider_id
  60.       WHERE kw.removed = false
  61.         AND kw.is_visible = true
  62.       GROUP BY kpi.kpi_id, s.main_spider_id, s.spidername
  63.       UNION
  64.       SELECT kpi.kpi_id,
  65.              s.main_spider_id                 AS spider_id,
  66.              s.spidername,
  67.              'products_fp_kpi_keywords'::text AS module,
  68.              array_agg(kw.id)                 AS keywords
  69.       FROM kpi_searchproductsonfirstpagekpi_keywords kkw
  70.                JOIN search_analytics_rpckeywordpreference ks ON kkw.rpckeyword_id = ks.keyword_id
  71.                JOIN search_analytics_rpckeyword kw ON kw.id = ks.keyword_id
  72.                JOIN kpi_searchproductsonfirstpagekpi kpi ON kkw.searchproductsonfirstpagekpi_id = kpi.id
  73.                JOIN (SELECT si.id AS main_spider_id,
  74.                             ss.id AS search_spider_id,
  75.                             si.spidername
  76.                      FROM items_spider si
  77.                               LEFT JOIN items_spider ss ON (si.spidername::text || '_search'::text) = ss.spidername::text AND ss.type = 3
  78.                      WHERE si.type = 1) s ON ks.spider_id = s.search_spider_id
  79.       WHERE kw.removed = false
  80.         AND kw.is_visible = true
  81.       GROUP BY kpi.kpi_id, s.main_spider_id, s.spidername
  82.       UNION
  83.       SELECT kpi.kpi_id,
  84.              s.main_spider_id                  AS spider_id,
  85.              s.spidername,
  86.              'products_top_kpi_keywords'::text AS module,
  87.              array_agg(kw.id)                  AS keywords
  88.       FROM kpi_searchproductsontopkpi_keywords kkw
  89.                JOIN search_analytics_rpckeywordpreference ks ON kkw.rpckeyword_id = ks.keyword_id
  90.                JOIN search_analytics_rpckeyword kw ON kw.id = ks.keyword_id
  91.                JOIN kpi_searchproductsontopkpi kpi ON kkw.searchproductsontopkpi_id = kpi.id
  92.                JOIN (SELECT si.id AS main_spider_id,
  93.                             ss.id AS search_spider_id,
  94.                             si.spidername
  95.                      FROM items_spider si
  96.                               LEFT JOIN items_spider ss ON (si.spidername::text || '_search'::text) = ss.spidername::text AND ss.type = 3
  97.                      WHERE si.type = 1) s ON ks.spider_id = s.search_spider_id
  98.       WHERE kw.removed = false
  99.         AND kw.is_visible = true
  100.       GROUP BY kpi.kpi_id, s.main_spider_id, s.spidername) sub
  101. GROUP BY sub.kpi_id, sub.spider_id, sub.spidername;
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement