如果有人有时间回答,请快速提问。当前查询效果很好,但我还需要获得订单总数和总运费。我知道由于连接,数字会被抛弃。
我知道我的数量和金额将是:
count(DISTINCT orders.id) AS num_orders,
SUM(orders.shipping_cost_ex_tax) AS shipping
我想我需要在原始选择中使用count和sum并在连接中处理其余部分,但是对于我的生活,我无法做到这一点。
任何帮助都会受到赞赏,即使它是“运行单独的查询”。谢谢大家。
当前查询:
SELECT
IF(products.categories LIKE '68', 'Shirts', 'Books') AS group_key,
CONCAT(order_products.name) AS product_name,
brands.name AS author,
SUM(order_products.quantity) AS num_units,
CASE WHEN products.sku LIKE '%-WB' THEN 'Combo'
WHEN products.sku LIKE '%-BO' THEN 'Box'
ELSE ''
END AS item_type,
SUM(IF(order_products.discount IS NULL, order_products.price_ex_tax, (order_products.price_ex_tax - order_products.discount))) AS income
FROM orders
INNER JOIN order_products ON order_products.bc_order_id = orders.bc_id
INNER JOIN products ON order_products.bc_product_id = products.bc_id
INNER JOIN brands ON products.brand_id = brands.bc_id
WHERE (orders.created_at BETWEEN '2012-01-28 00:00:00' and '2012-02-21 23:00:00')
GROUP BY group_key,
case when products.brand_id = '68'
then products.name
else products.sku
end
答案 0 :(得分:0)
查看提供的评论,而不是在我面前展示您的完整架构。这样的事情会起作用吗?
Table Report
(
id,
countOrders,
countSales,
countShipping,
countTax,
datePublished
)
Table SoldProducts
(
id,
price,
tax,
shippingPrice,
datePurchased
)
因此,您在此实例中要做的是通过从SoldProducts
查询生成报告,然后您将保留生成的报告。