code_junkie

Time slicing in Oracle/SQL

Nov 13th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. SELECT user_name, truncate(event_time, 'YYYYMMDD HH24MI'), count(*)
  2. FROM job_table
  3. WHERE event_time > TO_DATE( some start date time)
  4. AND user_name IN ( list of users to query )
  5. GROUP BY user_name, truncate(event_time, 'YYYYMMDD HH24MI')
  6.  
  7. Alice 2008-12-16 08:00 20
  8. Alice 2008-12-16 08:01 40
  9.  
  10. SELECT
  11. to_char(Times.time,'hh24:mi'),
  12. count(*)
  13. FROM
  14. (SELECT
  15. time
  16. FROM
  17. dual
  18. WHERE
  19. 1=2
  20. MODEL
  21. dimension by ( 0 as key )
  22. measures ( sysdate -1 as time )
  23. rules upsert ( time[ for key from 0 to 1 increment (1/1440) ] = sysdate-1 + cv(key)) ) Times,
  24. job_table
  25. WHERE
  26. begintime <= Times.time
  27. AND
  28. endtime > Times.time
  29. AND
  30. user_name = 'Alice'
  31. GROUP BY
  32. Times.time
  33. ORDER BY
  34. Times.time
Add Comment
Please, Sign In to add comment