Skip to content

Instantly share code, notes, and snippets.

@sauloperez
Created May 11, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauloperez/7ea6ebef793e7150fb01bb15f0574672 to your computer and use it in GitHub Desktop.
Save sauloperez/7ea6ebef793e7150fb01bb15f0574672 to your computer and use it in GitHub Desktop.
Order cycle management report in a single SQL query
select spree_line_items.order_id, spree_products.supplier_id, sum(spree_line_items.price * spree_line_items.quantity) AS amount
from spree_line_items
inner
join spree_variants
on spree_variants.id = spree_line_items.variant_id
inner
join spree_products
on spree_products.id = spree_variants.product_id
inner
join enterprises
on enterprises.id = spree_products.supplier_id
WHERE (
NOT (lower("public"."enterprises"."name") like '%prueba%')
AND NOT (lower("public"."enterprises"."name") like '%test%')
AND NOT (lower("public"."enterprises"."email_address") like '%prova%')
AND NOT (lower("public"."enterprises"."email_address") like '%mailinator.com%')
AND NOT (lower("public"."enterprises"."email_address") like '%prueba%')
AND NOT (lower("public"."enterprises"."email_address") like '%test%')
AND "public"."enterprises"."is_primary_producer" = TRUE
AND date_trunc('month', CAST("public"."enterprises"."created_at" AS timestamp)) = date_trunc('month', CAST((NOW() + INTERVAL '-1 month') AS timestamp))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment