Skip to content

Instantly share code, notes, and snippets.

@ecogswell
Created December 17, 2012 20:04
Show Gist options
  • Save ecogswell/4321576 to your computer and use it in GitHub Desktop.
Save ecogswell/4321576 to your computer and use it in GitHub Desktop.
SELECT
`main_table`.`customer_id`,
`main_table`.`customer_firstname` AS `firstname`,
`main_table`.`customer_lastname` AS `lastname`,
`distributor`.`name` AS `distributor_name`,
(select COUNT(distinct MONTH(created_at)) from sales_flat_order where customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `order_months_count`,
(select sum(total_qty_ordered) from sales_flat_order where customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `total_qty_ordered`,
(select sum(base_grand_total) from sales_flat_order where customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `total_grand_total`,
sum(MONTH(main_table.created_at) = 1) AS `Jan_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 1 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Jan_total`,
sum(MONTH(main_table.created_at) = 2) AS `Feb_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 2 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Feb_total`,
sum(MONTH(main_table.created_at) = 3) AS `Mar_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 3 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Mar_total`,
sum(MONTH(main_table.created_at) = 4) AS `Apr_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 4 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Apr_total`,
sum(MONTH(main_table.created_at) = 5) AS `May_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 5 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `May_total`,
sum(MONTH(main_table.created_at) = 6) AS `Jun_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 6 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Jun_total`,
sum(MONTH(main_table.created_at) = 7) AS `Jul_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 7 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Jul_total`,
sum(MONTH(main_table.created_at) = 8) AS `Aug_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 8 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Aug_total`,
sum(MONTH(main_table.created_at) = 9) AS `Sep_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 9 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Sep_total`,
sum(MONTH(main_table.created_at) = 10) AS `Oct_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 10 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Oct_total`,
sum(MONTH(main_table.created_at) = 11) AS `Nov_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 11 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Nov_total`,
sum(MONTH(main_table.created_at) = 12) AS `Dec_count`,
(select sum(base_subtotal) from sales_flat_order where month(created_at) = 12 and customer_id=main_table.customer_id and created_at between '0000-00-00' and NOW()) AS `Dec_total`
FROM
`sales_flat_order` AS `main_table`
INNER JOIN `purinapro_groups_distributor_order` AS `distributor_link`
ON main_table.entity_id = distributor_link.order_id
INNER JOIN `purinapro_groups_distributor` AS `distributor`
ON distributor_link.distributor_id = distributor.id
WHERE
(main_table.customer_id IS NOT NULL) AND
(main_table.created_at between '0000-00-00' and NOW())
GROUP BY
`main_table`.`customer_id`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment