Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE orders (id SERIAL,
- customerID INTEGER,
- timestamp BIGINT,
- PRIMARY KEY(id));
- CREATE VIEW lastOrders AS SELECT id,
- customerID,
- MAX(timestamp)
- FROM orders
- GROUP BY customerID;
- CREATE VIEW lastOrders AS
- SELECT
- DISTINCT ON (customerID)
- id,
- customerID,
- timestamp
- FROM orders
- ORDER BY customerID, timestamp desc;
- CREATE VIEW last_orders AS
- SELECT id, customer_id, timestamp
- FROM orders AS o
- WHERE timestamp = (
- SELECT MAX(timestamp)
- FROM orders AS oi
- WHERE o.customer_id = oi.customer_id
- );
- select a.*
- from
- orders a,
- (select customerID, max(timestamp) timestamp
- from orders group by customerID
- ) b
- where a.customer_id = b.customerID
- and a.timestamp = b.timestamp
Add Comment
Please, Sign In to add comment