zotov-vs

dwh_categories

Jun 2nd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.35 KB | None | 0 0
  1. CREATE OR REPLACE ALGORITHM = MERGE VIEW dwh_categories AS
  2.  
  3. SELECT CAST(c.categories_id AS INT) AS categories_id
  4.     , CAST(IFNULL(c.suppliers_id,0) AS INT) AS suppliers_id
  5.     , CAST(IFNULL(c.suppliers_contracts_id,0) AS INT) AS suppliers_contracts_id
  6.     , CAST(DATE_FORMAT(IFNULL(c.date_start, '0000-00-00'),'%Y%m%d') AS INT) AS date_start_id
  7.     , CAST(DATE_FORMAT(IFNULL(c.date_end, '0000-00-00'),'%Y%m%d') AS INT) AS date_end_id
  8.     , CAST(DATE_FORMAT(IFNULL(c.date_start, '0000-00-00'),'%H%i00') AS INT) AS time_start_id
  9.     , CAST(DATE_FORMAT(IFNULL(c.date_end, '0000-00-00'),'%H%i00') AS INT) AS time_end_id
  10.     , IFNULL(DATEDIFF(c.date_end,c.date_start),0) AS categories_duration
  11.     , c.categories_status AS categories_status_id
  12.     , cd.categories_name AS categories_name
  13.     , cd.categories_heading_title AS categories_title
  14.     , IFNULL(c.categories_parent, c.categories_id) AS categories_parent_id
  15.     , IFNULL(cdp.categories_name, cd.categories_name) AS categories_parent_name
  16.     , IF(c.categories_parent IS NULL OR c.categories_parent = 0, 1, 0) AS is_main_categories
  17.     , IFNULL(cr12.customers_id,0) AS content_employees_id
  18.     , IFNULL(c.customers_id,0)  AS brand_employees_id
  19.     , IFNULL(cr28.customers_id,0) AS analitic_employees_id
  20.     , (SELECT
  21.             COUNT(DISTINCT cbtp.classifier_age_groups_id) AS count_age_groups
  22.         FROM classifier_binds_to_products AS cbtp
  23.             JOIN products_to_categories AS ptc ON cbtp.object_id = ptc.products_id
  24.         WHERE ptc.categories_id = c.categories_id  
  25.         ) AS count_age_groups
  26.     , (SELECT
  27.             COUNT(DISTINCT cbtp.classifier_categories_id) AS count_categories
  28.         FROM classifier_binds_to_products AS cbtp
  29.             JOIN products_to_categories AS ptc ON cbtp.object_id = ptc.products_id
  30.         WHERE ptc.categories_id = c.categories_id  
  31.         ) AS count_categories
  32.     , (SELECT
  33.             COUNT(DISTINCT btc.brands_id) AS count_brands
  34.         FROM brands_to_categories AS btc
  35.         WHERE btc.categories_id = c.categories_id  
  36.         ) AS count_brands      
  37.  
  38. --  , IFNULL(c.categories_types_id,0) AS categories_types_id
  39.     , cf.categories_forms_name as categories_types_name
  40.     -- , IFNULL(c.categories_types_id,0) AS categories_stock_types_id
  41.     , ct.categories_types_name as categories_stock_name
  42.     , IFNULL(cg.classifier_groups_name, '-') AS categories_priority_groups_name
  43.  
  44. FROM categories AS c
  45.     JOIN categories_description AS cd ON cd.categories_id = c.categories_id
  46.     JOIN categories_status AS cs ON cs.status_id = c.categories_status
  47.     -- контент-менеджер
  48.     LEFT JOIN categories_responsible AS cr12 ON cr12.categories_id = c.categories_id AND cr12.acl_roles_id = 12
  49.    
  50.     LEFT JOIN categories_responsible AS cr28 ON cr28.categories_id = c.categories_id AND cr28.acl_roles_id = 28
  51.     -- LEFT JOIN customers AS c28 ON c28.customers_id = cr28.customers_id
  52.     LEFT JOIN categories_description as cdp on cdp.categories_id = c.categories_parent
  53. LEFT JOIN categories_types as ct on ct.categories_types_id = c.categories_types_id
  54. LEFT JOIN categories_forms as cf on cf.categories_forms_id = c.categories_forms_id
  55. LEFT JOIN classifier_groups as cg on cg.classifier_groups_id = c.classifier_groups_id
  56.  
  57. JOIN dwh_upload_config AS duc on duc.component = 'dwh_categories'
  58. WHERE
  59.     (duc.date_from_use = 0 OR c.last_date_update >= duc.date_from)
  60.     AND (duc.date_to_use = 0 OR c.last_date_update <= duc.date_to)
  61.     AND (duc.id_from_use = 0 OR c.categories_id >= duc.id_from)
  62.     AND (duc.id_to_use = 0 OR c.categories_id <= duc.id_to)
  63. ;
Add Comment
Please, Sign In to add comment