2016-01-19 201 views
-1

我嘗試遵循提供的解決方案here,但它在我的情況下不起作用。GROUP BY + MAX(date)

1)在此是我嘗試,但提出下面的錯誤的第一解決方案:

SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.SKU, maxdate) WHERE inv.outbound_date IS NOT NULL AND co.system='Magent' at line 26

SELECT 
     inv.inventory_id, 
     pu.purchase_order_id, 
     inv.inbound_date, 
     inv.outbound_date, 
     siz.SKU, 
     cat.name, 
     pu.cogs, 
     pu.cogs_currency, 
     co.system, 
     co.order_number 
    FROM ERP_INVENTORY_ITEM inv 
     INNER JOIN ERP_PROD_PRODUCT_SIZES siz ON siz.ID = inv.prod_index 
     INNER JOIN ERP_PURCHASE_ITEM pu ON pu.`Index` = inv.ERP_PURCHASE_ITEM_Index 
     INNER JOIN ERP_PROD_CATALOG cat ON cat.`index` = siz.`SKUf ID` 
     INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 coi ON coi.`index` = inv.ERP_CUSTOMER_ORDER_ITEMS_2_index_outbound 
     INNER JOIN ERP_CUSTOMER_ORDER co ON co.`index` = coi.ERP_CUSTOMER_ORDER_index 

     INNER JOIN (
      SELECT siz2.SKU, MAX(inv2.inbound_date) AS maxdate 
      FROM ERP_INVENTORY_ITEM inv2 
      INNER JOIN ERP_PROD_PRODUCT_SIZES siz2 ON siz2.ID = inv2.prod_index 
      GROUP BY siz2.SKU 
     ) AS max USING (siz2.SKU, maxdate) 

    WHERE inv.outbound_date IS NOT NULL 
     AND co.system='Magento' 
     AND inv.item_warehouse='KL' 
     AND pu.cogs IS NOT NULL 
     AND LOCATE('-',co.order_number)=FALSE 
     AND siz.SKU IN 
      (
      'SPRA0148002R0303-54', 
      'SRYB2658004P0303-50', 
      'SOAK0632006R0105-55', 
      'SRYB0372080R0707-50', 
      'SRYB0226048P1406-58', 
      'SRBJ0030048R1406-50', 
      'SOAK0281046R0203-70', 
      'SRYB2671020R1406-58', 
      'SVAL0170005R0101-51', 
      'SOAK0091035R0506-56', 
      'SMAU0038046P1205-71', 
      'GVER054500501-53', 
      'GRYB009302401-52', 
      'AC05-88N0275Z00000Z', 
      'BF02-87N0400N12518Z', 
      'BF02-87N1000N07502Z', 
      'GCAR019705306-54', 
      'PF01-86Z0000Z00000Z', 
      'BBT05-86N0150N20018Z', 
      'AC11-87N0425Z00000Z', 
      'BF01-86N0350Z00000Z', 
      'PUV03-87N0700N07516Z', 
      'BF02-87N0100N12518Z', 
      'BF02-87N0750N22517Z', 
      'BF02-87N0550N12509Z', 
      'AC02-85N0300Z00000Z', 
      'PC02-86N0650Z00000Z', 
      'AC06-86N0550N22518Z', 
      'BM03-87N0250N17512Z', 
      'CV53-86N0300Z00000Z', 
      'PUV03-87N0550N17509Z', 
      'BBT04-86N0250N25002Z', 
      'BI20-86Z0000Z00000Z', 
      'SGMV0612005R0101-00', 
      'VE04-86N0400Z00000Z', 
      'GSFD020111-59', 
      'FL17-86N0225Z00000Z', 
      'FL18-86N0325Z00000Z', 
      'GMC02-86N0375Z00000Z', 
      'BE06-86N0175Z00000Z', 
      'PM01-86Z0000Z00000Z', 
      'PM02-86N0450Z00000Z', 
      'GMC55-87N0650Z00000Z', 
      'GCN05-87N0350Z00000Z', 
      'GMC01-86N0300Z00000Z' 
      ) 
    ORDER BY siz.SKU; 

2)在此是第二溶液我嘗試,但沒有按結果的數量」 t匹配結果的數量如果我做一個簡單的GROUP BY

SELECT 
    inv.inventory_id, 
    pu.purchase_order_id, 
    inv.inbound_date, 
    inv.outbound_date, 
    siz.SKU, 
    cat.name, 
    pu.cogs, 
    pu.cogs_currency, 
    co.system, 
    co.order_number 
FROM ERP_INVENTORY_ITEM inv 
    INNER JOIN ERP_PROD_PRODUCT_SIZES siz ON siz.ID = inv.prod_index 
    INNER JOIN ERP_PURCHASE_ITEM pu ON pu.`Index` = inv.ERP_PURCHASE_ITEM_Index 
    INNER JOIN ERP_PROD_CATALOG cat ON cat.`index` = siz.`SKUf ID` 
    INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 coi ON coi.`index` = inv.ERP_CUSTOMER_ORDER_ITEMS_2_index_outbound 
    INNER JOIN ERP_CUSTOMER_ORDER co ON co.`index` = coi.ERP_CUSTOMER_ORDER_index 
WHERE inv.outbound_date IS NOT NULL 
    AND inv.inbound_date = 
     (
     SELECT MAX(inv2.inbound_date) 
     FROM ERP_INVENTORY_ITEM inv2 
     INNER JOIN ERP_PROD_PRODUCT_SIZES siz2 ON siz2.ID=inv2.prod_index 
     WHERE siz2.SKU=siz.SKU 
     ) 
    AND co.system='Magento' 
    AND inv.item_warehouse='KL' 
    AND pu.cogs IS NOT NULL 
    AND LOCATE('-',co.order_number)=FALSE 
    AND siz.SKU IN 
     (
    'SPRA0148002R0303-54', 
    'SRYB2658004P0303-50', 
    'SOAK0632006R0105-55', 
    'SRYB0372080R0707-50', 
    'SRYB0226048P1406-58', 
    'SRBJ0030048R1406-50', 
    'SOAK0281046R0203-70', 
    'SRYB2671020R1406-58', 
    'SVAL0170005R0101-51', 
    'SOAK0091035R0506-56', 
    'SMAU0038046P1205-71', 
    'GVER054500501-53', 
    'GRYB009302401-52', 
    'AC05-88N0275Z00000Z', 
    'BF02-87N0400N12518Z', 
    'BF02-87N1000N07502Z', 
    'GCAR019705306-54', 
    'PF01-86Z0000Z00000Z', 
    'BBT05-86N0150N20018Z', 
    'AC11-87N0425Z00000Z', 
    'BF01-86N0350Z00000Z', 
    'PUV03-87N0700N07516Z', 
    'BF02-87N0100N12518Z', 
    'BF02-87N0750N22517Z', 
    'BF02-87N0550N12509Z', 
    'AC02-85N0300Z00000Z', 
    'PC02-86N0650Z00000Z', 
    'AC06-86N0550N22518Z', 
    'BM03-87N0250N17512Z', 
    'CV53-86N0300Z00000Z', 
    'PUV03-87N0550N17509Z', 
    'BBT04-86N0250N25002Z', 
    'BI20-86Z0000Z00000Z', 
    'SGMV0612005R0101-00', 
    'VE04-86N0400Z00000Z', 
    'GSFD020111-59', 
    'FL17-86N0225Z00000Z', 
    'FL18-86N0325Z00000Z', 
    'GMC02-86N0375Z00000Z', 
    'BE06-86N0175Z00000Z', 
    'PM01-86Z0000Z00000Z', 
    'PM02-86N0450Z00000Z', 
    'GMC55-87N0650Z00000Z', 
    'GCN05-87N0350Z00000Z', 
    'GMC01-86N0300Z00000Z' 
    ) 
ORDER BY siz.SKU; 

任何hel p會很感激。

Thx。

+2

由於'max'是保留字,所以將別名'max'更改爲別的。 ')AS max USING'可能是''AS max_val USING' –

+0

如錯誤信息所示,問題出現在'.SKU'上。刪除表名('siz2'),使它成爲'USING(SKU,maxdate)'。 – axiac

+0

@Abhik Chakraborty我改變了它,但我面臨着同樣的錯誤。 – seeusoon

回答

0

這裏是「幾乎」工作解決方案,我至今:
不會引發錯誤,但我得到13分的結果,而不是17
我得到17分的結果,如果我刪除最後一個INNER JOIN並添加只是一個GROUP BY siz.SKU

謝謝。

SELECT 
     inv.inventory_id, 
     pu.purchase_order_id, 
     inv.inbound_date, 
     inv.outbound_date, 
     siz.SKU, 
     cat.name, 
     pu.cogs, 
     pu.cogs_currency, 
     co.system, 
     co.order_number 
    FROM ERP_INVENTORY_ITEM inv 
     INNER JOIN ERP_PROD_PRODUCT_SIZES siz ON siz.ID = inv.prod_index 
     INNER JOIN ERP_PURCHASE_ITEM pu ON pu.`Index` = inv.ERP_PURCHASE_ITEM_Index 
     INNER JOIN ERP_PROD_CATALOG cat ON cat.`index` = siz.`SKUf ID` 
     INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 coi ON coi.`index` = inv.ERP_CUSTOMER_ORDER_ITEMS_2_index_outbound 
     INNER JOIN ERP_CUSTOMER_ORDER co ON co.`index` = coi.ERP_CUSTOMER_ORDER_index 

     INNER JOIN (
      SELECT siz2.SKU AS SKU_2, MAX(inv2.inbound_date) AS maxdate 
      FROM ERP_INVENTORY_ITEM inv2 
      INNER JOIN ERP_PROD_PRODUCT_SIZES siz2 ON siz2.ID = inv2.prod_index 
      GROUP BY siz2.SKU 
     ) AS max_val ON siz.SKU = max_val.SKU_2 AND inv.inbound_date = max_val.maxdate 

    WHERE inv.outbound_date IS NOT NULL 
     AND co.system='Magento' 
     AND inv.item_warehouse='KL' 
     AND pu.cogs IS NOT NULL 
     AND LOCATE('-',co.order_number)=FALSE 
     AND siz.SKU IN 
      (
      'SPRA0148002R0303-54', 
      'SRYB2658004P0303-50', 
      'SOAK0632006R0105-55', 
      'SRYB0372080R0707-50', 
      'SRYB0226048P1406-58', 
      'SRBJ0030048R1406-50', 
      'SOAK0281046R0203-70', 
      'SRYB2671020R1406-58', 
      'SVAL0170005R0101-51', 
      'SOAK0091035R0506-56', 
      'SMAU0038046P1205-71', 
      'GVER054500501-53', 
      'GRYB009302401-52', 
      'AC05-88N0275Z00000Z', 
      'BF02-87N0400N12518Z', 
      'BF02-87N1000N07502Z', 
      'GCAR019705306-54', 
      'PF01-86Z0000Z00000Z', 
      'BBT05-86N0150N20018Z', 
      'AC11-87N0425Z00000Z', 
      'BF01-86N0350Z00000Z', 
      'PUV03-87N0700N07516Z', 
      'BF02-87N0100N12518Z', 
      'BF02-87N0750N22517Z', 
      'BF02-87N0550N12509Z', 
      'AC02-85N0300Z00000Z', 
      'PC02-86N0650Z00000Z', 
      'AC06-86N0550N22518Z', 
      'BM03-87N0250N17512Z', 
      'CV53-86N0300Z00000Z', 
      'PUV03-87N0550N17509Z', 
      'BBT04-86N0250N25002Z', 
      'BI20-86Z0000Z00000Z', 
      'SGMV0612005R0101-00', 
      'VE04-86N0400Z00000Z', 
      'GSFD020111-59', 
      'FL17-86N0225Z00000Z', 
      'FL18-86N0325Z00000Z', 
      'GMC02-86N0375Z00000Z', 
      'BE06-86N0175Z00000Z', 
      'PM01-86Z0000Z00000Z', 
      'PM02-86N0450Z00000Z', 
      'GMC55-87N0650Z00000Z', 
      'GCN05-87N0350Z00000Z', 
      'GMC01-86N0300Z00000Z' 
      ) 
    ORDER BY siz.SKU 
    ;