3

Magentoの次のMySQLクエリを変更しようとしています。現在は、レポート用のシンプルで構成可能な製品の製品情報を返します。残念ながら、単純な製品には、親の構成可能な製品が持つすべての情報が含まれているわけではありません。

このテーブルcatalog_product_super_linkを使用して、すべての製品の親製品IDを検索し、すべての製品について、構成可能な親製品のデータを取得できるようにします。スーパーリンクテーブルの1つの列にはすべての製品IDの値があり、別の列には対応する親があります。構成可能な製品の場合、製品IDと親IDは同じです。

わかる?

私が持っているクエリは次のとおりです。

SELECT IFNULL(category_name.value, category_default_name.value) AS `primarycategory`
     , `product`.`sku`
     , IFNULL(product_name.value, product_default_name.value) AS `product_name`
     , `attribute_option_value`.`value` AS `story`
     , (
         select GROUP_CONCAT(category_name.value)
         from catalog_category_entity as category
            , catalog_category_entity_varchar as category_name
            , catalog_category_product as category_product
         WHERE category_name.entity_id = category.entity_id
           AND category_name.store_id = 0
           AND category_name.entity_type_id = 3
           AND category_name.attribute_id = 33
           AND category_product.product_id = order_item.product_id
           AND category_product.category_id = category.entity_id
           AND category_name.value<>'Default Category'
       ) AS `category`
     , round(sum(order_item.qty_ordered)) AS `unitssoldnum`
     , ROUND(sum(order_item.price-order_item.discount_amount-order_item.tax_amount)
           , 2
       ) AS `unitssoldcur`
     , ROUND((
               SUM(order_item.price-order_item.discount_amount-order_item.tax_amount)
               - (IFNULL(product_cost.value, product_default_cost.value) * SUM(order_item.qty_ordered))
             )
           , 2
       ) AS `grossmargincur`
     , ROUND((
               (
                 SUM(order_item.price-order_item.discount_amount-order_item.tax_amount)
                 - (IFNULL(product_cost.value, product_default_cost.value) * SUM(order_item.qty_ordered))
               )
               / (SUM(order_item.price-order_item.discount_amount-order_item.tax_amount))
             )
             * 100
           , 2
       ) AS `grossmarginpercent`
     , (
         SELECT round(SUM(qty))
         FROM cataloginventory_stock_item AS stock
            , catalog_product_super_link AS product_link
         WHERE product_link.product_id = stock.product_id
           AND product_link.parent_id=order_item.product_id
       ) AS `sohatstore`
     , (
         SELECT round(SUM(qty)/SUM(order_item.qty_ordered))
         FROM cataloginventory_stock_item AS stock
            , catalog_product_super_link AS product_link
         WHERE product_link.product_id = stock.product_id
           AND product_link.parent_id=order_item.product_id
       ) AS `storecover`
     , (
         SELECT round(SUM(order_item.qty_ordered)
                      /(SUM(qty)+SUM(order_item.qty_ordered))
                      *100
                    , 2
                )
         FROM cataloginventory_stock_item AS stock
            , catalog_product_super_link AS product_link
         WHERE product_link.product_id = stock.product_id
           AND product_link.parent_id=order_item.product_id
       ) AS `sellthrupercent`
     , ROUND(SUM(order_item.price),2) AS `originalretail`
     , ROUND(SUM(IFNULL(product_cost.value, product_default_cost.value))
           , 2
       ) AS `costextax`
     , (
         SELECT round(SUM(qty_ordered))
         FROM sales_flat_order_item AS order_total
         WHERE order_total.product_id = order_item.product_id
       ) AS `totalsoldtilldate`
     , (
         SELECT ROUND((
                        SELECT round(SUM(qty_ordered))
                        FROM sales_flat_order_item AS order_total
                        WHERE order_total.product_id = order_item.product_id
                      )
                      /(
                        (
                          SELECT round(SUM(qty_ordered))
                          FROM sales_flat_order_item AS order_total
                          WHERE order_total.product_id = order_item.product_id
                        )
                        +(
                          SELECT round(SUM(qty))
                          FROM cataloginventory_stock_item AS stock
                             , catalog_product_super_link AS product_link
                          WHERE product_link.product_id = stock.product_id
                            AND product_link.parent_id=order_item.product_id
                        )
                      )*100
                    , 2
                )
       ) AS `totalsellthru`
     , (
         SELECT ROUND((
                        (
                          sum(order_total.price-order_total.discount_amount-order_total.tax_amount)
                          - sum(order_total.base_cost * order_total.qty_ordered)
                        )
                        /sum(order_total.price-order_total.discount_amount-order_total.tax_amount)
                      )*100
                    , 2
                )
         FROM sales_flat_order_item AS order_total
         WHERE order_total.product_id = order_item.product_id
       ) AS `totalgrossmarginpercent`
FROM `sales_flat_order` AS `order`
 INNER JOIN `sales_flat_order_item` AS `order_item` ON order_item.order_id = order.entity_id
 INNER JOIN `catalog_product_entity` AS `product`
    ON product.entity_id = order_item.product_id
   AND product.entity_type_id = 4
   AND product.type_id NOT IN('grouped', 'bundle')
 LEFT JOIN `catalog_product_entity_varchar` AS `product_name`
    ON product_name.entity_id = product.entity_id
   AND product_name.store_id = order.store_id
   AND product_name.entity_type_id = 4
   AND product_name.attribute_id = 60
 LEFT JOIN `catalog_product_entity_varchar` AS `product_default_name`
    ON product_default_name.entity_id = product.entity_id
   AND product_default_name.store_id = 0
   AND product_default_name.entity_type_id = 4
   AND product_default_name.attribute_id = 60
 LEFT JOIN `catalog_product_entity_decimal` AS `product_cost`
    ON product_cost.entity_id = product.entity_id
   AND product_cost.store_id = order.store_id
   AND product_cost.entity_type_id = 4
   AND product_cost.attribute_id = 68
 LEFT JOIN `catalog_product_entity_decimal` AS `product_default_cost`
    ON product_default_cost.entity_id = product.entity_id
   AND product_default_cost.store_id = 0
   AND product_default_cost.entity_type_id = 4
   AND product_default_cost.attribute_id = 68
 LEFT JOIN `catalog_category_entity` AS `category`
    ON category.entity_id =  (select MAX(category_id) from catalog_category_product where product_id = product.entity_id)
   AND category.entity_type_id = 3
 LEFT JOIN `catalog_category_entity_varchar` AS `category_name`
    ON category_name.entity_id = category.parent_id
   AND category_name.store_id = order.store_id
   AND category_name.entity_type_id = 3
   AND category_name.attribute_id = 33
 LEFT JOIN `catalog_category_entity_varchar` AS `category_default_name`
    ON category_default_name.entity_id = category.parent_id
   AND category_default_name.store_id = 0
   AND category_default_name.entity_type_id = 3
   AND category_default_name.attribute_id = 33
 LEFT JOIN `catalog_product_entity_int` AS `story_name`
    ON story_name.entity_id = product.entity_id
   AND story_name.store_id = 1
   AND story_name.entity_type_id = 4
   AND story_name.attribute_id = 138
 LEFT JOIN `catalog_product_entity_int` AS `story_default_name`
    ON story_default_name.entity_id = product.entity_id
   AND story_default_name.store_id = 0
   AND story_default_name.entity_type_id = 4
   AND story_default_name.attribute_id = 138
 LEFT JOIN `eav_attribute_option` AS `attribute_option`
    ON attribute_option.option_id = IFNULL(story_name.value, story_default_name.value)
 LEFT JOIN `eav_attribute_option_value` AS `attribute_option_value`
    ON attribute_option_value.option_id = attribute_option.option_id
   AND attribute_option_value.store_id = 0
WHERE (order.state <> 'canceled')
  AND (DATE(order.created_at) <= DATE('2012-07-03'))
  AND (DATE(order.created_at) >= DATE('2012-07-01'))
GROUP BY `product_name`
ORDER BY `category_name`.`value` ASC
       , `category_default_name`.`value` ASC
       , `product_name`.`value` ASC
       , `product_default_name`.`value` ASC

誰かが私を正しい方向に向けることができれば、それは大いにありがたいです:)

構成可能な製品について得られる出力のサンプルを次に示します。

"Category Name",Product,Name,Story,"Category Description","Units Sold (#)","Units Sold ($)","GM ($)","GM (%)","SOH at Store","Store Cover","Sell through %","Original Retail Price","Cost Ex Tax","Total Units Sold (#)","Total Sell Thru %","GM% Total"
Bottoms,31875,"Denim Babe Jean",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$39.25,65.59,118,59,1.67,$68.00,$10.30,81,40.70,69.38
Bottoms,34475,"Diamante Rip Boot Leg Jean",DENIM,"Bottoms,SALE,Denim,Jeans,Bootleg Jean,Pants",2,$29.92,-$7.56,-25.25,28,14,6.67,$34.00,$18.74,5,15.15,19.90
Bottoms,35853,"Embroidered Back Wide Leg Jean",DENIM,"Bottoms,SALE,Denim,Jeans,Wideleg Jean,Pants",2,$29.92,-$9.08,-30.35,11,6,15.38,$34.00,$19.50,22,66.67,23.93
Bottoms,34278,"Firecracker Bootleg Jean",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$29.92,50.00,9,5,18.18,$68.00,$14.96,39,81.25,43.99
Bottoms,28450,"Glamourzon Jean","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$59.84,100.00,101,51,1.94,$68.00,$0.00,51,33.55,100.00

そして、これが私が単純な製品で得たもののサンプルです。欠落しているすべてのデータに注意してください。そのため、私のレポートでは、親製品IDを検索し、そのデータを使用したいと思います。

"Category Name",Product,Name,Story,"Category Description","Units Sold (#)","Units Sold ($)","GM ($)","GM (%)","SOH at Store","Store Cover","Sell through %","Original Retail Price","Cost Ex Tax","Total Units Sold (#)","Total Sell Thru %","GM% Total"
Bottoms,2000004614430,"Denim Babe Jean - Dark Denim - 24",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$20.59,,,,,$0.00,$10.30,15,,
Bottoms,2000004901523,"Diamante Rip Boot Leg Jean - Black - 14",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$37.48,,,,,$0.00,$18.74,2,,
Bottoms,2000004883881,"Firecracker Bootleg Jean - Denim Dark - 16",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$29.92,,,,,$0.00,$14.96,9,,
Bottoms,2000004159955,"Glamourzon Jean - Dark Denim - 24","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,$0.00,,,,,$0.00,$0.00,11,,
Bottoms,2005010849432,"Glamourzon Night Jean - Black - 28","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$32.80,,,,,$0.00,$16.40,7,,-68.82

それがあなたの質問に答えたことを願っています。ありがとう!

4

1 に答える 1

0

同様のレポートをいくつか行っています。するべきこと:

  • シンプルな商品からスタート
  • 構成可能な製品のcatalog_product_super_linkテーブルでJOINします
  • クエリの SELECT 部分で、単純な製品の値がそこにある場合は IF => を使用し、そうでない場合は構成可能な製品の値を使用します (またはその逆)。

SELECT
    o.increment_id,    
    oi.order_id,
    o.STATUS AS order_status,
    o.created_at,                  
    ROUND(SUM(IF(oi2.qty_ordered IS NOT NULL, IF(oi2.qty_ordered > oi.qty_ordered, oi2.qty_ordered, oi.qty_ordered), oi.qty_ordered)), 0) AS cqty_ordered,
    ROUND(SUM(IF(oi2.qty_canceled IS NOT NULL, IF(oi2.qty_canceled > oi.qty_canceled, oi2.qty_canceled, oi.qty_canceled), oi.qty_canceled)), 0) AS cqty_canceled,
    ROUND(SUM(IF(oi2.qty_shipped IS NOT NULL, IF(oi2.qty_shipped > oi.qty_shipped, oi2.qty_shipped, oi.qty_shipped), oi.qty_shipped)), 0) AS cqty_shipped,
    ROUND(SUM(IF(oi2.qty_ordered IS NOT NULL, IF(oi2.qty_ordered > oi.qty_ordered, oi2.qty_ordered, oi.qty_ordered), oi.qty_ordered)) - SUM(IF(oi2.qty_canceled IS NOT NULL, IF(oi2.qty_canceled > oi.qty_canceled, oi2.qty_canceled, oi.qty_canceled), oi.qty_canceled)) - SUM(IF(oi2.qty_shipped IS NOT NULL, IF(oi2.qty_shipped > oi.qty_shipped, oi2.qty_shipped, oi.qty_shipped), oi.qty_shipped)), 0) AS cqty_missing
FROM sales_flat_order_item AS oi
INNER JOIN sales_flat_order AS o ON oi.order_id=o.entity_id
LEFT JOIN sales_flat_order_item AS oi2 ON oi.parent_item_id=oi2.item_id AND oi2.product_type='configurable'
WHERE oi.product_type='simple'
GROUP BY oi.order_id
HAVING cqty_missing=0 AND order_status NOT IN ('complete','canceled','closed');
于 2013-01-03T14:16:37.807 に答える