我正在寻找一个 MySQL 查询(子查询很好),它将以以下格式获得过去一年中每个订单的单位分布:
units_per_order | number_of_orders | percent_of_total -------------------------------------------------- ------------------ 1 | 7500 | 55% 2 3 4 5 6-10 10-20 30-50 50–100 100+
编辑:
要查询的表和必要的列:
表:订单 字段:orders_id、date_purchased 表:orders_products 字段:orders_id、products_quantity
这就是我现在所拥有的:
选择计数(*)作为数量,数量来自( 选择 SUM(products_quantity) 作为数量,orders_id FROM orders_products op LEFT JOIN orders o USING (orders_id) WHERE date_purchased LIKE '2012%' GROUP BY orders_id ) AS new_table WHERE qty != 0 GROUP BY qty ORDER BY qty
有任何想法吗?