从下表中我如何获得过去几天销售额> gt = = 50%的商品?
date item_id in_stock out-stock
2011-06-20 352 50 30
2011-06-21 351 10 1
2011-06-22 332 23 20
2011-06-23 311 12 7
目前我正在使用一个计算销售百分比的查询,并使用php我循环浏览每件商品,并获得销售额超过50%的商品。
SELECT i.id as item_id,i.item,SUM(dpr.out_stock)/SUM(dpr.in_stock) * 100 as p,SUM(dpr.out_stock) as sales,u.title as unit,u.id as unit_id
FROM `sm_daily_item_stock_report` as dpr
INNER JOIN sm_item_master as i on dpr.item_id=i.id and i.consumable='1'
INNER JOIN sm_unit_master as u on i.primary_unit=u.id
WHERE date between '2011-06-20' and '2011-06-23'
and dpr.store_id='1' GROUP BY item_id
但是此查询会针对store_id 1返回所有产品。
Krishnik
答案 0 :(得分:2)
将HAVING p > 50
添加到查询中:)
答案 1 :(得分:0)
SELECT i.id as item_id,i.item,SUM(dpr.out_stock)/SUM(dpr.in_stock) * 100 as p,SUM(dpr.out_stock) as sales,u.title as unit,u.id as unit_id
FROM `sm_daily_item_stock_report` as dpr
INNER JOIN sm_item_master as i on dpr.item_id=i.id and i.consumable='1'
INNER JOIN sm_unit_master as u on i.primary_unit=u.id
WHERE date between '2011-06-20' and '2011-06-23'
and dpr.store_id='1' GROUP BY item_id
having (SUM(dpr.out_stock)/SUM(dpr.in_stock) * 100) > 50