Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. WITH
  2. daily_updates AS ( --My synthetic 100-record impression of your date table
  3. SELECT level AS entry_number
  4. , SYSDATE + level + DBMS_RANDOM.VALUE/2 AS insertion_timestamp
  5. FROM dual
  6. CONNECT BY level<100),
  7. update_times AS ( --Conversion of dates to times-since-midnight
  8. SELECT NUMTODSINTERVAL(TO_CHAR(insertion_timestamp,'MI'),'MINUTE') --Extract minutes from date/time, and convert to time interval
  9. + NUMTODSINTERVAL(TO_CHAR(insertion_timestamp,'HH24'),'HOUR') --Extract hours from date/time, and convert to time interval
  10. AS tsm --Sum of hour and minute components: TSM (Time Since Midnight)
  11. FROM daily_updates)
  12. SELECT max(tsm)-min(tsm) AS time_range --The part you're looking for
  13. , min(tsm) AS earliest_TSM --In case you're curious
  14. , max(tsm) AS latest_TSM --What the min/max TSMs are
  15. FROM update_times
  16.  
  17. DECLARE
  18. FUNCTION get_time_span(verbose IN VARCHAR2 DEFAULT NULL)
  19. RETURN INTERVAL DAY TO SECOND
  20. IS
  21. CURSOR date_entries
  22. IS SELECT insertion_timestamp
  23. FROM daily_updates; --Your date table here
  24. min_TSM INTERVAL DAY TO SECOND;
  25. max_TSM INTERVAL DAY TO SECOND;
  26. current_TSM INTERVAL DAY TO SECOND;
  27. BEGIN
  28. FOR entry IN date_entries LOOP
  29. current_TSM := NUMTODSINTERVAL(TO_CHAR(entry.insertion_timestamp, 'MI'), 'MINUTE')
  30. + NUMTODSINTERVAL(TO_CHAR(entry.insertion_timestamp, 'HH24'), 'HOUR');
  31. IF min_TSM IS NULL OR current_TSM < min_TSM THEN
  32. min_TSM := current_TSM;
  33. END IF;
  34. IF max_TSM IS NULL OR current_TSM > max_TSM THEN
  35. max_TSM := current_TSM;
  36. END IF;
  37. END LOOP;
  38. RETURN max_TSM - min_TSM;
  39. END get_time_span;
  40. BEGIN
  41. DBMS_OUTPUT.PUT_LINE('The time span is '||get_time_span());
  42. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement