Guest User

Untitled

a guest
Jun 25th, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. CREATE TABLE orders (id SERIAL,
  2. customerID INTEGER,
  3. timestamp BIGINT,
  4. PRIMARY KEY(id));
  5.  
  6. CREATE VIEW lastOrders AS SELECT id,
  7. customerID,
  8. MAX(timestamp)
  9. FROM orders
  10. GROUP BY customerID;
  11.  
  12. CREATE VIEW lastOrders AS
  13. SELECT
  14. DISTINCT ON (customerID)
  15. id,
  16. customerID,
  17. timestamp
  18. FROM orders
  19. ORDER BY customerID, timestamp desc;
  20.  
  21. CREATE VIEW last_orders AS
  22. SELECT id, customer_id, timestamp
  23. FROM orders AS o
  24. WHERE timestamp = (
  25. SELECT MAX(timestamp)
  26. FROM orders AS oi
  27. WHERE o.customer_id = oi.customer_id
  28. );
  29.  
  30. select a.*
  31. from
  32. orders a,
  33. (select customerID, max(timestamp) timestamp
  34. from orders group by customerID
  35. ) b
  36. where a.customer_id = b.customerID
  37. and a.timestamp = b.timestamp
Add Comment
Please, Sign In to add comment