Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. CREATE TEMP TABLE customer_ids ON COMMIT DROP AS
  2. SELECT DISTINCT customer_id FROM orders
  3. WHERE payment_status IN (1,300) AND
  4. date_ordered BETWEEN '06/26/2011' AND '07/02/2011' AND
  5. customer_id != 0;
  6.  
  7. CREATE INDEX tmp_cid_idx ON customer_ids (customer_id);
  8. ANALYZE customer_ids;
  9.  
  10. CREATE TEMP TABLE last_orders ON COMMIT DROP AS SELECT id, customer_id, date_ordered FROM orders
  11. WHERE payment_status = 1 AND
  12. date_ordered BETWEEN '6/1/2010' AND now() AND
  13. customer_id IN (SELECT customer_id FROM customer_ids);
  14.  
  15. --Then one of the following
  16. SELECT o.id, o.customer_id, o.date_ordered, f.print_group_id, f.item_id, f.cost
  17. FROM last_orders o
  18. INNER JOIN files f ON o.id = f.order_id AND f.page = 1;
  19.  
  20. --OR
  21. CREATE TEMP TABLE last_files ON COMMIT DROP AS
  22. SELECT order_id, print_group_id, item_id, cost
  23. FROM files
  24. WHERE order_id IN (SELECT id FROM last_orders);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement