按mySQL中的产品类型和商店总计销售数据

时间:2017-01-30 15:53:46

标签: mysql

我很难写一份声明来提取多家商店的销售数据并将其显示为摘要。我在这里查询4个表格以获取我需要的所有信息。它工作正常,直到我想要推销某些类型的产品。

继承我的陈述:

SELECT s.name, ROUND(sum(od.price*od.quantity),2) as total, SUM(od.quantity) as items, ROUND(sum(od.price*od.quantity),2)/SUM(od.quantity) as aiv, COUNT(o.serial) as orders, ROUND(sum(od.price*od.quantity),2)/COUNT(o.serial) as aov, SUM(CASE WHEN p.type = 'B' THEN ROUND(sum(od.price*od.quantity),2) ELSE 0 END) as bikesales FROM stores as s 
JOIN orders as o ON s.name = o.store
JOIN order_detail as od ON o.serial = od.orderid
JOIN products as p ON od.productid = p.id
WHERE s.live!=0 AND o.date BETWEEN '2015-01-01' AND '2017-12-31' AND (o.status=1 OR o.status=2 OR o.status=3 OR o.status=4) AND od.status=1
GROUP BY s.name
ORDER BY s.name

我认为这是阻止它正常工作的分组,因为我得到的错误是Invalid use of group function,但它是商店名称分组所必需的。

按产品类型选择的部分位于:SUM(CASE WHEN p.type = 'B' THEN ROUND(sum(od.price*od.quantity),2) END) as bikesales - 我正在尝试获取所有自行车销售总额并将其显示为bikesales

这可能吗?

1 个答案:

答案 0 :(得分:0)

嵌套求和函数是非法的,试试这个:

SUM(CASE WHEN p.type = 'B' THEN ROUND(od.price*od.quantity,2) ELSE 0 END) as bikesales /*Without other ROUND(sum(od.price*od.quantity),2) */