Advertisement
Guest User

Untitled

a guest
May 25th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. DROP TABLE IF EXISTS transaction CASCADE;
  2.  
  3. CREATE TABLE transaction (
  4. times INTEGER PRIMARY KEY,
  5. ta Integer,
  6. object VARCHAR(1),
  7. access VARCHAR(5),
  8. CHECK(access = 'read' OR access = 'write')
  9. );
  10.  
  11. INSERT INTO transaction
  12. (times, ta, object, access)
  13. VALUES
  14. (1, 1, 'A', 'read'),
  15. (2, 2, 'B', 'read'),
  16. (3, 2, 'B', 'write'),
  17. (4, 3, 'A', 'read'),
  18. (5, 3, 'C', 'write'),
  19. (6, 2, 'C', 'read'),
  20. (7, 1, 'B', 'read'),
  21. (8, 3, 'D', 'write');
  22.  
  23.  
  24. SELECT a.object, a.times AS A_Time, a.ta AS A_Trans, b.times AS B_Time, b.ta AS B_Trans
  25. FROM transaction a
  26. JOIN transaction b ON a.object = b.object
  27. JOIN transaction c ON a.ta = c.ta
  28. WHERE a.ta <> b.ta
  29. AND b.times > a.times
  30. AND c.times > b.times
  31. ;
  32. WITH Startend AS (SELECT MIN(a.times) AS start, MAX(a.times) AS ende, a.ta
  33. FROM transaction a JOIN transaction b USING (ta)
  34. GROUP BY ta),
  35. r AS (
  36. SELECT 1 AS times, 1 AS ta, 'A'::VARCHAR AS object
  37. UNION ALL
  38. SELECT b.times, b.ta, b.object
  39. FROM r
  40. JOIN transaction b on b.times > r.times )
  41.  
  42. Select *
  43. from startend;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement