Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 11.02 KB | None | 0 0
  1. # TODO vorjahres, vormonat zahlen fehlen, muss irgendwie aus dem datum kontext berechnet werden
  2.  
  3. SET @from_date := '01.03.2020';
  4. SET @to_date := '31.03.2020';
  5.  
  6. SELECT delivery_slips_items.article_sub_number                      AS 'Cat. No.',
  7.        articles.ean                                                 AS 'EAN-Code',
  8.        articles_sub_types.name                                      AS 'Format',
  9.        articles.status                                              AS 'Status',
  10.        articles.article_disponents_id                               AS 'Disponenten-Nr.',
  11.        articles.product_code_id                                     AS 'Produkte-Code',
  12.        articles.ue_code                                             AS 'U / E Code',
  13.        articles.genre                                               AS 'Musik- / Filmart',
  14.        articles.brand_block_id                                      AS 'Block',
  15.        LPAD(articles.brand_main_id, 2, '0')                         AS 'Marke',
  16.        brand_blocks.brand_name                                      AS 'Markenbezeichnung',
  17.        LPAD(articles.brand_label_code_id, 2, '0')                   AS 'Label',
  18.        brand_blocks.label_name                                      AS 'Labelbezeichnung',
  19.        articles.accounting_nb                                       AS 'Erloeskonto',
  20.        articles.alphapart                                           As 'Alpha',
  21.        articles.supplier_article_nb                                 As 'Artikel-Nr. Lieferant',
  22.        artists.names                                                AS 'Kuenstler',
  23.        dirigent.names                                               AS 'Direigent',
  24.        orchestra.names                                              AS 'Orchester',
  25.        composer.names                                               AS 'Komponist',
  26.        articles.diverses                                            AS 'Diverses',
  27.        articles.title                                               AS 'Title',
  28.        articles.set_units                                           AS 'Anhzahl Einheiten Lieferant',
  29.        delivery_slips_items.price_code                              AS 'Pricecode',
  30.        delivery_slips_items.price_tier                              AS 'Price Tier',
  31.        ROUND(delivery_slips_items.ppd, 2)                           AS 'PPD',
  32.        delivery_slips_items.client_nb                               AS 'Cust.No.',
  33.        SUBSTRING(
  34.                clients.short_address,
  35.                1,
  36.                LENGTH(clients.short_address) - LENGTH(REPLACE(clients.short_address, ' ', '')) + 5
  37.            )                                                        AS 'CN short',
  38.        clients.short_address                                        AS 'Customer Name',
  39.        sum(delivery_slips_items.revenue_rabat_value)                AS 'Umsatzrab.',
  40.        sum(delivery_slips_items.performance_rabat_value)            AS 'Leistungsrab.',
  41.        sum(delivery_slips_items.special_rabat_value)                AS 'Spezialrab.',
  42.        ROUND(delivery_slips_items.netto_price, 2)                   AS 'Warenwert / Unit',
  43.        articles.units_1_month_ago                                   AS 'Bestand begin',
  44.        sum(IF(
  45.                delivery_slips_items.order_type NOT REGEXP '^([R-TEIJ])$',
  46.                delivery_slips_items.units_normalized,
  47.                0
  48.            ))                                                       AS 'Sales Quantity',
  49.  
  50.        sum(IF(
  51.                delivery_slips_items.order_type REGEXP '^[R-S]$',
  52.                delivery_slips_items.units_normalized,
  53.                0
  54.            ))                                                       AS 'Returns Quantity',
  55.        sum(IF(
  56.                delivery_slips_items.order_type REGEXP '^[EIT]$',
  57.                delivery_slips_items.units_normalized,
  58.                0
  59.            ))                                                       AS 'Promo Quantity',
  60.        sum(IF(
  61.                delivery_slips_items.client_nb = 94234,
  62.                delivery_slips_items.units_normalized,
  63.                0
  64.            ))                                                       AS 'Vernichtungen Quantity',
  65.        sum(IF(
  66.                delivery_slips_items.client_nb = 94226,
  67.                delivery_slips_items.units_normalized,
  68.                0
  69.            ))                                                       AS 'Returen an lieferant Quantity',
  70.  
  71.        articles.units_2_month_ago                                      AS 'Bestand end',
  72.        sum(IF(
  73.                delivery_slips_items.order_type NOT REGEXP '^([R-TEI])$',
  74.                delivery_slips_items.netto_price,
  75.                0
  76.            ))                                                       AS 'Sales CHF',
  77.        sum(IF(
  78.                delivery_slips_items.order_type REGEXP '^[R-S]$',
  79.                delivery_slips_items.netto_price,
  80.                0
  81.            ))                                                       AS 'Returns CHF',
  82.        sum(IF(
  83.                delivery_slips_items.order_type REGEXP '^[EIT]$',
  84.                delivery_slips_items.netto_price,
  85.                0
  86.            ))                                                       AS 'Promo CHF',
  87.        sum(ROUND(delivery_slips_items.total_value_netto * 0.97, 2)) AS 'Formel',
  88.        articles.additional3                                         AS 'Lizenzsatz',
  89.        articles.accounting_nb                                       AS 'KST',
  90.        articles.suisa_nb                                            AS 'Suisa NB'
  91. FROM (
  92.          SELECT *,
  93.                 IF(
  94.                         delivery_slips_items.spcial_price != 0,
  95.                         delivery_slips_items.spcial_price,
  96.                         prices.price
  97.                     )       AS ppd,
  98.                 prices.name AS price_tier,
  99.                 IF(
  100.                         delivery_slips_items.order_type REGEXP '^[R-T]$',
  101.                         delivery_slips_items.units,
  102.                         delivery_slips_items.delivered
  103.                     )       AS units_normalized
  104.          FROM delivery_slips_items
  105.                   LEFT JOIN
  106.               (
  107.                   SELECT code,
  108.                          value AS price,
  109.                          name
  110.                   FROM prices
  111.               ) AS prices
  112.               ON delivery_slips_items.price_code = prices.code
  113.      ) AS delivery_slips_items
  114.  
  115.  
  116.          LEFT JOIN articles
  117.                    ON delivery_slips_items.article_sub_number = articles.article_sub_number
  118.          LEFT JOIN
  119.      (
  120.          SELECT article_type_id,
  121.                 article_subtype_id,
  122.                 name
  123.          FROM articles_sub_types
  124.      ) AS articles_sub_types
  125.      ON articles.article_type_id = articles_sub_types.article_type_id
  126.          AND articles.article_subtype_id = articles_sub_types.article_subtype_id
  127.          LEFT JOIN
  128.      (
  129.          SELECT brand_blocks.brand_block_id AS brand_block_id,
  130.                 brand_mains.brand_main_id   AS brand_main_id,
  131.                 brand_mains.brand_name      AS brand_name,
  132.                 brand_mains.label_code_id   AS label_code_id,
  133.                 brand_mains.label_name      AS label_name
  134.          FROM brand_blocks
  135.                   LEFT JOIN
  136.               (
  137.                   SELECT brand_mains.brand_block_id AS block_id,
  138.                          brand_mains.brand_main_id  AS brand_main_id,
  139.                          brand_mains.text           AS brand_name,
  140.                          brand_labels.code_id       AS label_code_id,
  141.                          brand_labels.name          AS label_name
  142.                   FROM brand_mains
  143.                            LEFT JOIN
  144.                        (
  145.                            SELECT code_id,
  146.                                   brand_main_text_id,
  147.                                   name
  148.                            FROM brand_labels
  149.                        ) AS brand_labels
  150.                        ON brand_mains.id = brand_labels.brand_main_text_id
  151.               ) AS brand_mains
  152.               ON brand_blocks.id = brand_mains.block_id
  153.      ) AS brand_blocks
  154.      ON articles.brand_block_id = brand_blocks.brand_block_id
  155.          AND articles.brand_main_id = brand_blocks.brand_main_id
  156.          AND articles.brand_label_code_id = brand_blocks.label_code_id
  157.          LEFT JOIN
  158.      (
  159.          SELECT article_id,
  160.                 GROUP_CONCAT(contributors.name SEPARATOR ' / ') AS names
  161.          FROM contributors_roles
  162.                   LEFT JOIN
  163.               (
  164.                   SELECT id,
  165.                          name
  166.                   FROM contributors
  167.               ) AS contributors
  168.               ON contributors.id = contributors_roles.contributors_id
  169.          WHERE contributors_roles.role_id = 1
  170.          GROUP BY contributors_roles.article_id
  171.      ) AS artists
  172.      ON articles.id = artists.article_id
  173.          LEFT JOIN
  174.      (
  175.          SELECT article_id,
  176.                 GROUP_CONCAT(contributors.name SEPARATOR ' / ') AS names
  177.          FROM contributors_roles
  178.                   LEFT JOIN
  179.               (
  180.                   SELECT id,
  181.                          name
  182.                   FROM contributors
  183.               ) AS contributors
  184.               ON contributors.id = contributors_roles.contributors_id
  185.          WHERE contributors_roles.role_id = 2
  186.          GROUP BY contributors_roles.article_id
  187.      ) AS dirigent
  188.      ON articles.id = dirigent.article_id
  189.          LEFT JOIN
  190.      (
  191.          SELECT article_id,
  192.                 GROUP_CONCAT(contributors.name SEPARATOR ' / ') AS names
  193.          FROM contributors_roles
  194.                   LEFT JOIN
  195.               (
  196.                   SELECT id,
  197.                          name
  198.                   FROM contributors
  199.               ) AS contributors
  200.               ON contributors.id = contributors_roles.contributors_id
  201.          WHERE contributors_roles.role_id = 4
  202.          GROUP BY contributors_roles.article_id
  203.      ) AS orchestra
  204.      ON articles.id = orchestra.article_id
  205.          LEFT JOIN
  206.      (
  207.          SELECT article_id,
  208.                 GROUP_CONCAT(contributors.name SEPARATOR ' / ') AS names
  209.          FROM contributors_roles
  210.                   LEFT JOIN
  211.               (
  212.                   SELECT id,
  213.                          name
  214.                   FROM contributors
  215.               ) AS contributors
  216.               ON contributors.id = contributors_roles.contributors_id
  217.          WHERE contributors_roles.role_id = 3
  218.          GROUP BY contributors_roles.article_id
  219.      ) AS composer
  220.      ON articles.id = composer.article_id
  221.          LEFT JOIN
  222.      (
  223.          SELECT nb,
  224.                 short_address
  225.          FROM clients
  226.      ) AS clients
  227.      ON delivery_slips_items.client_nb = clients.nb
  228. WHERE delivery_slips_items.report_day >= STR_TO_DATE(@from_date, '%d.%m.%Y')
  229.   AND delivery_slips_items.report_day <= STR_TO_DATE(@to_date, '%d.%m.%Y')
  230.   AND delivery_slips_items.units_normalized != 0
  231.   and articles.brand_block_id = 'B'
  232.   and articles.article_sub_number = 'BELLA947V'
  233. group by delivery_slips_items.article_sub_number
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement