Guest User

Untitled

a guest
Aug 13th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. How to find repeated, timestamped data
  2. create table issue_attributes (
  3. issue_id number,
  4. attr_timestamp timestamp,
  5. attribute_name varchar2(500),
  6. attribute_value varchar2(500),
  7. CONSTRAINT ia-pk PRIMARY KEY (issue_id, attr_timestamp, attribute_name)
  8. )
  9.  
  10. select issue_id, attr_timestamp, attribute_name, attribute_value
  11. from issue_attributes where issue_id = 1 and attribute_name = 'OWNER';
  12.  
  13. 1, 01-JAN-2011 12:00, 'OWNER', 'john.doe@example.com'
  14. 1, 01-FEB-2011 12:00, 'OWNER', 'john.doe@example.com'
  15. 1, 01-MAR-2011 12:00, 'OWNER', 'john.doe@example.com'
  16. 1, 01-APR-2011 12:00, 'OWNER', 'john.doe@example.com'
  17.  
  18. 1, 01-JAN-2011 12:00, 'OWNER', 'john.doe@example.com'
  19.  
  20. 2, 01-JAN-2011 12:00, 'OWNER', 'john.doe@example.com'
  21. 2, 01-FEB-2011 12:00, 'OWNER', 'jane.deere@example.com'
  22. 2, 01-MAR-2011 12:00, 'OWNER', 'john.doe@example.com'
  23. 2, 01-APR-2011 12:00, 'OWNER', 'john.doe@example.com'
  24.  
  25. 2, 01-JAN-2011 12:00, 'OWNER', 'john.doe@example.com'
  26. 2, 01-FEB-2011 12:00, 'OWNER', 'jane.deere@example.com'
  27. 2, 01-MAR-2011 12:00, 'OWNER', 'john.doe@example.com'
  28.  
  29. with issue_attributes as (
  30. select 1 as issue_id, date '2011-01-01' as attr_timestamp,
  31. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  32. select 1 as issue_id, date '2011-02-01' as attr_timestamp,
  33. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  34. select 1 as issue_id, date '2011-03-01' as attr_timestamp,
  35. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  36. select 1 as issue_id, date '2011-04-01' as attr_timestamp,
  37. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  38. select 2 as issue_id, date '2011-01-01' as attr_timestamp,
  39. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  40. select 2 as issue_id, date '2011-02-01' as attr_timestamp,
  41. 'OWNER' as attribute_name, 'jane.deere@example.com' as attribute_value from dual union all
  42. select 2 as issue_id, date '2011-03-01' as attr_timestamp,
  43. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual union all
  44. select 2 as issue_id, date '2011-04-01' as attr_timestamp,
  45. 'OWNER' as attribute_name, 'john.doe@example.com' as attribute_value from dual
  46. )
  47. select
  48. issue_id,
  49. attr_timestamp,
  50. attribute_name,
  51. attribute_value,
  52. case when lag(attribute_value) over (partition by issue_id, attribute_name order by attr_timestamp) = attribute_value then null else 'Y'end as keep_value
  53. from
  54. issue_attributes
  55.  
  56. ISSUE_ID ATTR_TIMESTAMP ATTRIBUTE_NAME ATTRIBUTE_VALUE KEEP_VALUE
  57. 1 01/01/2011 OWNER john.doe@example.com Y
  58. 1 01/02/2011 OWNER john.doe@example.com
  59. 1 01/03/2011 OWNER john.doe@example.com
  60. 1 01/04/2011 OWNER john.doe@example.com
  61. 2 01/01/2011 OWNER john.doe@example.com Y
  62. 2 01/02/2011 OWNER jane.deere@example.com Y
  63. 2 01/03/2011 OWNER john.doe@example.com Y
  64. 2 01/04/2011 OWNER john.doe@example.com
  65.  
  66. SQL> desc issue_attributes
  67. Name Null? Type
  68. ----------------------------------------------------------------- -------- --------------------------------------------
  69. ISSUE_ID NUMBER
  70. ATTR_TIMESTAMP TIMESTAMP(6)
  71. ATTRIBUTE_NAME VARCHAR2(500)
  72. ATTRIBUTE_VALUE VARCHAR2(500)
  73.  
  74. SQL> select * from issue_attributes;
  75.  
  76. ISSUE_ID ATTR_TIMESTAMP ATTRIBUTE_ ATTRIBUTE_VALUE
  77. ---------- ----------------------------------- ---------- ------------------------------
  78. 1 01-JAN-20 11.12.00.000000 AM OWNER john.doe@example.com
  79. 1 01-FEB-20 11.12.00.000000 AM OWNER john.doe@example.com
  80. 1 01-MAR-20 11.12.00.000000 AM OWNER john.doe@example.com
  81. 1 01-APR-20 11.12.00.000000 AM OWNER john.doe@example.com
  82. 1 01-JAN-20 11.12.00.000000 AM OWNER john.doe@example.com
  83. 1 01-JAN-20 11.12.00.000000 AM OWNER john.doe@example.com
  84. 1 01-FEB-20 11.12.00.000000 AM OWNER jane.deere@example.com
  85. 1 01-MAR-20 11.12.00.000000 AM OWNER john.doe@example.com
  86. 1 01-APR-20 11.12.00.000000 AM OWNER john.doe@example.com
  87. 1 01-JAN-20 11.12.00.000000 AM OWNER john.doe@example.com
  88. 1 01-FEB-20 11.12.00.000000 AM OWNER jane.deere@example.com
  89. 1 01-MAR-20 11.12.00.000000 AM OWNER john.doe@example.com
  90.  
  91. 12 rows selected.
  92.  
  93. SQL> delete from issue_attributes
  94. where rowid in(select rid
  95. from (select rowid rid,
  96. row_number() over (partition by ISSUE_ID,
  97. ATTR_TIMESTAMP,
  98. ATTRIBUTE_NAME,
  99. ATTRIBUTE_VALUE
  100. order by rowid) rn
  101. from issue_attributes)
  102. where rn<> 1);
  103. 7 rows deleted.
  104.  
  105. SQL> select * from issue_attributes;
  106.  
  107. ISSUE_ID ATTR_TIMESTAMP ATTRIBUTE_ ATTRIBUTE_VALUE
  108. ---------- ----------------------------------- ---------- ------------------------------
  109. 1 01-JAN-20 11.12.00.000000 AM OWNER john.doe@example.com
  110. 1 01-FEB-20 11.12.00.000000 AM OWNER john.doe@example.com
  111. 1 01-MAR-20 11.12.00.000000 AM OWNER john.doe@example.com
  112. 1 01-APR-20 11.12.00.000000 AM OWNER john.doe@example.com
  113. 1 01-FEB-20 11.12.00.000000 AM OWNER jane.deere@example.com
  114.  
  115. 5 rows selected.
  116.  
  117. SELECT MAX(attr_timestamp), issue_id, attribute_name, attribute_value
  118. FROM issue_attributes
  119. GROUP BY issue_id, attribute_name, attribute_value
  120.  
  121. SET search_path='tmp';
  122.  
  123. -- The rows you want to delete.
  124. SELECT * FROM issue_attributes to_del
  125. WHERE EXISTS (
  126. SELECT * FROM issue_attributes xx
  127. WHERE xx.issue_id = to_del.issue_id
  128. AND xx.attribute_name = to_del.attribute_name
  129. AND xx.attribute_value = to_del.attribute_value
  130. AND xx.attr_timestamp > to_del.attr_timestamp
  131. AND NOT EXISTS ( SELECT * FROM issue_attributes nx
  132. WHERE nx.issue_id = to_del.issue_id
  133. AND nx.attribute_name = to_del.attribute_name
  134. AND nx.attribute_value <> to_del.attribute_value
  135. AND nx.attr_timestamp > to_del.attr_timestamp
  136. AND nx.attr_timestamp < xx.attr_timestamp
  137. )
  138. ) ;
  139.  
  140. -- For completeness: the rows you want to keep.
  141. SELECT * FROM issue_attributes must_stay
  142. WHERE NOT EXISTS (
  143. SELECT * FROM issue_attributes xx
  144. WHERE xx.issue_id = must_stay.issue_id
  145. AND xx.attribute_name = must_stay.attribute_name
  146. AND xx.attribute_value = must_stay.attribute_value
  147. AND xx.attr_timestamp > must_stay.attr_timestamp
  148. AND NOT EXISTS ( SELECT * FROM issue_attributes nx
  149. WHERE nx.issue_id = must_stay.issue_id
  150. AND nx.attribute_name = must_stay.attribute_name
  151. AND nx.attribute_value <> must_stay.attribute_value
  152. AND nx.attr_timestamp > must_stay.attr_timestamp
  153. AND nx.attr_timestamp < xx.attr_timestamp
  154. )
  155. ) ;
  156.  
  157. issue_id | attr_timestamp | attribute_name | attribute_value
  158. ----------+---------------------+----------------+----------------------
  159. 1 | 2011-03-01 12:00:00 | OWNER | john.doe@example.com
  160. 1 | 2011-01-01 12:00:00 | OWNER | john.doe@example.com
  161. 1 | 2011-02-01 12:00:00 | OWNER | john.doe@example.com
  162. 2 | 2011-03-01 12:00:00 | OWNER | john.doe@example.com
  163. (4 rows)
  164.  
  165. issue_id | attr_timestamp | attribute_name | attribute_value
  166. ----------+---------------------+----------------+------------------------
  167. 1 | 2011-04-01 12:00:00 | OWNER | john.doe@example.com
  168. 2 | 2011-02-01 12:00:00 | OWNER | jane.deere@example.com
  169. 2 | 2011-04-01 12:00:00 | OWNER | john.doe@example.com
  170. 2 | 2011-01-01 12:00:00 | OWNER | john.doe@example.com
  171. (4 rows)
Add Comment
Please, Sign In to add comment