MySQL学习_查看各仓库产品的销售情况_20161102
订单表结构是具体到每个订单下面多个产品,而仓库出货的表结构是对每个订单的金额汇总 不区分订单产品
因此如果想计算每个仓库每个产品的销售情况 需要将两个表连接起来 并且产品是昨天在线且有库存的产品
#昨日在线且有库存的SKU SELECT a.城市,a.仓库,b.* ,SUM(a.订单额) AS 历史15天金额 ,SUM(a.销量) AS 历史15天销量 ,SUM(IF(a.订单日期>=DATE_ADD(CURRENT_DATE,INTERVAL - 7 DAY) AND a.订单日期<CURRENT_DATE,a.订单额,NULL)) AS 历史7天金额 ,SUM(IF(a.订单日期>=DATE_ADD(CURRENT_DATE,INTERVAL - 7 DAY) AND a.订单日期<CURRENT_DATE,a.销量,NULL)) AS 历史7天销量 ,SUM(IF(a.订单日期>=DATE_ADD(CURRENT_DATE,INTERVAL - 3 DAY) AND a.订单日期<CURRENT_DATE,a.订单额,NULL)) AS 历史3天金额 ,SUM(IF(a.订单日期>=DATE_ADD(CURRENT_DATE,INTERVAL - 3 DAY) AND a.订单日期<CURRENT_DATE,a.销量,NULL)) AS 历史3天销量 FROM (#以订单ID匹配仓库 具体到每笔订单每个产品ID SELECT a1.城市,a2.仓库,DATE(a1.订单日期) AS 订单日期,a1.订单ID,a1.产品ID,SUM(a1.金额) AS 订单额,SUM(汇总单品量) AS 销量 FROM `a003_order` AS a1 INNER JOIN `a018_cgwy_order_car` AS a2 ON a1.订单ID=a2.订单号 WHERE a1.城市="北京" AND a1.金额>0 AND a1.订单日期>=DATE_ADD(CURRENT_DATE,INTERVAL - 15 DAY) AND a1.订单日期<CURRENT_DATE GROUP BY a1.订单ID,a1.产品ID ) AS a INNER JOIN (#昨日在线且昨日有库存的产品 SELECT b1.产品ID,b3.商品名称,b3.单位,b1.单品售价,b3.转换率,b3.打包单位,b1.打包售价,b3.品牌,b3.库存总数,b3.库存打包数,b3.库存平均成本,b3.库存总额,b3.在线且有库存日期 FROM `a010_dynamic_sku` AS b1 INNER JOIN ( SELECT b2.城市,b2.产品ID,b2.商品名称,b2.品牌,b2.库存总数,b2.库存打包数,b2.库存平均成本,b2.库存总额,b2.单位,b2.打包单位,b2.转换率,DATE(b2.日期) AS 在线且有库存日期 FROM `a014_stock_daily` AS b2 WHERE b2.城市="北京" AND DATE(b2.日期)=DATE_ADD(CURRENT_DATE,INTERVAL - 1 DAY) AND b2.库存总数>0 AND b2.产品ID=42 GROUP BY b2.产品ID ) AS b3 ON b3.产品ID=b1.产品ID WHERE b1.城市="北京" AND DATE(b1.在线日期)=DATE_ADD(CURRENT_DATE,INTERVAL - 1 DAY) GROUP BY b1.产品ID ) AS b ON a.产品ID=b.产品ID GROUP BY a.城市,a.仓库,a.产品ID ORDER BY a.产品ID