GROUP BY + MAX(日期)

时间:2016-01-19 09:44:56

标签: mysql group-by max

我尝试按照here提供的解决方案,但在我的情况下不起作用。

1)这是我尝试的第一个解决方案,但引发了以下错误:

  

SQL错误(1064):您的SQL语法中有错误;检查   手册,对应右边的MySQL服务器版本   在'.SKU,maxdate'附近使用的语法WHERE inv.outbound_date IS NOT NULL   AND co.system ='Magent'在第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)这是我尝试的第二个解决方案,但如果我做一个简单的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;

任何帮助都会受到赞赏。

THX。

1 个答案:

答案 0 :(得分:0)

这是我到目前为止的“几乎”工作解决方案:
没有错误,但我得到13个结果,而不是17 如果我删除最后一个INNER JOIN并仅添加GROUP BY siz.SKU

,我会得到17个结果

感谢。

    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
    ;