Domanda

I have a requirement to make a sales report like this:

Product Name         First Week   Second Week  Third Week
   a                      12            6           2
   b                       0            0           4 
   c                       0            3           0  

For this I made a function which returns sku and sum(qty_ordered) like

   $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 query for the above is:

 `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 

But the above returns for only 1 week. If I change the created_at BETWEEN NOW()-INTERVAL 1 WEEK AND NOW() to created_at BETWEEN NOW()-INTERVAL 2 WEEK AND NOW() then it returns 2 weeks sales.. But I want the result as shown in the above format. So how can I customize this. Help me on this.

È stato utile?

Soluzione

`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` 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top