2011-06-24 78 views
0

從下表中我可以如何獲得最近幾天銷售額> = 50%的商品?MySql查詢:獲取銷售額> =特定百分比的商品

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

回答

2

添加HAVING p > 50到查詢:)

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 
相關問題