Skip to content

Instantly share code, notes, and snippets.

@shofetim
Created May 20, 2013 20:43
Show Gist options
  • Select an option

  • Save shofetim/5615372 to your computer and use it in GitHub Desktop.

Select an option

Save shofetim/5615372 to your computer and use it in GitHub Desktop.
-- This doesn't work as product.id is not grouped.
SELECT
meta.name AS "Name",
string_agg(DISTINCT product.code, ', ') AS "Product Codes",
(
SELECT COALESCE(SUM(line.quantity_ordered *
line.unit_price_at_checkout_time),
0)
FROM orders_orderline AS line
LEFT JOIN orders_order AS orders ON orders.id = line.order_id
WHERE line.product_id = product.id
AND
(orders.order_status >= 3
AND
orders.order_date BETWEEN '2013-04-01' AND '2013-04-02')
) AS "Total Quantity Ordered (in dollars)"
FROM warehouse_pieces_piecemeta AS meta
LEFT JOIN warehouse_pieces_piece AS piece
ON piece.piece_meta_id = meta.id
LEFT JOIN products_product AS product
ON product.piece_id = piece.id
WHERE
product.is_active = True
GROUP BY meta.id;
------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment