我要求制作这样的销售报告:
Product Name First Week Second Week Third Week
a 12 6 2
b 0 0 4
c 0 3 0
为此我创建了一个返回sku和sum(qty_ordered)的函数,如
$query = Mage::getResourceModel('sales/order_item_collection');
$query->getSelect()->reset(Zend_Db_Select::COLUMNS)
->columns(array('name','qty_ordered'))
->where(new Zend_Db_Expr('created_at BETWEEN NOW()-INTERVAL 1 WEEK AND NOW()'))
->group(array('sku'));
$query->getSelect()->columns(array(
'week1' => 'SUM(main_table.qty_ordered)')
);
上面的SQL查询是:
`SELECT `main_table`.`sku`, `main_table`.`qty_ordered`,
SUM(main_table.qty_ordered) AS `week1`
FROM `sales_flat_order_item` AS `main_table`
WHERE (created_at BETWEEN NOW()-INTERVAL 1 WEEK AND NOW())
GROUP BY `sku
但上述回报仅持续1周。如果我将created_at BETWEEN NOW()-INTERVAL 1 WEEK AND NOW()
更改为created_at BETWEEN NOW()-INTERVAL 2 WEEK AND NOW()
,则会返回2周的销售额。但我希望结果如上所示。那么我该如何定制呢。帮助我。
答案 0 :(得分:2)
`SELECT `main_table`.`sku`, `main_table`.`qty_ordered`,
SUM(main_table.qty_ordered) AS `week1`,
SUM(if (created_at BETWEEN NOW()-INTERVAL 2 WEEK AND NOW(), main_table.qty_ordered,0)) AS `week2`,
SUM(if (created_at BETWEEN NOW()-INTERVAL 3 WEEK AND NOW(), main_table.qty_ordered,0)) AS `week3`
FROM `sales_flat_order_item` AS `main_table`
WHERE (created_at BETWEEN NOW()-INTERVAL 3 WEEK AND NOW())
GROUP BY `sku`