mysql总和案例不起作用

时间:2016-12-07 22:10:11

标签: mysql sum case

我正在使用sum case处理mysql查询。该查询适用于某些情况,但不适用于一种情况。

    select orders.orderid, campers.description, dealers.name, orders.act_delivery, 
sum(case orderitems.std 
when 0 then 
    (case when orderitems.price = 0 then retail_price.price * orderitems. qty else  orderitems.price * orderitems.qty end)
when 1 then 
    (case when orderitems.origqty = 0 then 
        0 else 
        (case when orderitems.price = 0 
            then retail_price.price * (orderitems.qty - orderitems.origqty)   
        else  orderitems.price * (orderitems.qty - orderitems.origqty)
        end)
    end) 

when 2 then
    0
else 
    (case when orderitems.deletedprice = 0 then retail_price.price * orderitems. qty else  orderitems.deletedprice * orderitems.qty end)

end) as retail
 from orderitems, orders, dealers, campers, products, retail_price 
 where orderitems.orderid = orders.orderid 
 and orderitems.productid = products.prodid 
 and orderitems.productid =retail_price.prodid
 and orders.dealerid = retail_price.dealer 
 and orders.dealerid = dealers.dealerid 
 and orders.camper = campers.camperid 
 and act_delivery > 0 
 and orderitems.std in (0, 1, 2) 
 and orders.dealerid not in (1, 9, 10, 12,15,16) 
 and spares_od = 0 
 and orders.act_delivery > '2016-10-01' 
 and orders.act_delivery <= '2016-10-31' 
 group by orders.orderid,orders.dealerid, orders.act_delivery order by orders.act_delivery, orders.orderid

当orderitems.std = 3时会出现问题。在任何一种情况下,我都无法得到结果。

所有其他情况似乎都正常。

0 个答案:

没有答案