我尝试解决问题,但我需要帮助:)
实际上这个查询有效,我得到了最后一个列中所有销售额的总和:
SELECT DISTINCT epps.SKU, epc.name ,
COUNT(*) FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
WHERE (eco.state = "processing" OR eco.state= "complete")
AND eco.created_at > DATE_ADD("2015-11-06",INTERVAL 12 HOUR)
AND eco.created_at < DATE_ADD("2015-12-06",INTERVAL 12 HOUR)
AND epc.attribute_set = "Contact Lenses"
AND eco.order_location = "KL"
GROUP BY epps.SKU
ORDER BY COUNT(*) DESC
我想要每30天销售一次的产品数量。我也想在上个月销售的产品数量。但它不起作用。这是我的新查询:
我想比较2个不同月份的销售额
SELECT DISTINCT epps.SKU, epc.name ,
(SELECT COUNT(*) FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
WHERE (eco.state = "processing" OR eco.state= "complete")
AND eco.created_at > DATE_ADD("2015-11-06",INTERVAL 12 HOUR)
AND eco.created_at < DATE_ADD("2015-12-06",INTERVAL 12 HOUR)
AND epc.attribute_set = "Contact Lenses"
AND eco.order_location = "KL"
),
(SELECT COUNT(*) FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
WHERE (eco.state = "processing" OR eco.state= "complete")
AND eco.created_at > DATE_ADD("2015-10-06",INTERVAL 12 HOUR)
AND eco.created_at < DATE_ADD("2015-11-06",INTERVAL 12 HOUR)
AND epc.attribute_set = "Contact Lenses"
AND eco.order_location = "KL"
)
FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
GROUP BY epps.SKU
ORDER BY COUNT(*) DESC
我使用了大致相同的框架,但我的结果与以前不一样。
非常感谢! 纪尧姆
答案 0 :(得分:0)
表演是否是您的主要关注点?如果没有,使用两个子查询并加入它们可能会很好。这样的事情(未经测试):
SELECT * from
(
SELECT DISTINCT epps.SKU, epc.name ,
COUNT(*) FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
WHERE (eco.state = "processing" OR eco.state= "complete")
AND eco.created_at > DATE_ADD("2015-11-06",INTERVAL 12 HOUR)
AND eco.created_at < DATE_ADD("2015-12-06",INTERVAL 12 HOUR)
AND epc.attribute_set = "Contact Lenses"
AND eco.order_location = "KL"
GROUP BY epps.SKU
ORDER BY COUNT(*) DESC
) a
inner join
(
SELECT DISTINCT epps.SKU, epc.name ,
COUNT(*) FROM ERP_CUSTOMER_ORDER eco
INNER JOIN ERP_CUSTOMER_ORDER_ITEMS_2 ecoi2 ON ecoi2.ERP_CUSTOMER_ORDER_index = eco.`index`
INNER JOIN ERP_PROD_PRODUCT_SIZES epps ON ecoi2.prod_index = epps.ID
INNER JOIN ERP_PROD_CATALOG epc ON epps.`SKUf ID`= epc.`index`
WHERE (eco.state = "processing" OR eco.state= "complete")
AND eco.created_at > DATE_ADD("2015-10-06",INTERVAL 12 HOUR)
AND eco.created_at < DATE_ADD("2015-11-06",INTERVAL 12 HOUR)
AND epc.attribute_set = "Contact Lenses"
AND eco.order_location = "KL"
GROUP BY epps.SKU
ORDER BY COUNT(*) DESC
) b
on a.SKU = b.SKU and a.name = b.name
请注意,如果您的数据中没有零值,则此查询将无效(例如,对于没有销售任何镜头的月份,如果您没有行,则此查询将在任何一个月都不返回任何结果因为内部联接)。