Guest User

Untitled

a guest
Jan 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. SELECT count(*) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour
  2. FROM accounts
  3. WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-Aug-12','DD-Mon-RR')
  4. GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24')
  5. ORDER BY app_date, app_hour
  6.  
  7. SELECT NVL(c.num_apps,0) as num_apps, t.app_date, t.app_hour
  8. FROM time_table t
  9. LEFT OUTER JOIN
  10. (
  11. SELECT count(*) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour
  12. FROM accounts
  13. WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-Aug-12','DD-Mon-RR')
  14. GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24')
  15. ORDER BY app_date, app_hour
  16. ) c ON (t.app_date = c.app_date AND t.app_hour = c.app_hour)
  17.  
  18. select level
  19. FROM Dual
  20. CONNECT BY level <= 10
  21. ORDER BY level;
  22.  
  23. select 0 as num_apps, (To_Date('16-09-12','DD-MM-RR') + level / 24) as created_ts
  24. FROM dual
  25. CONNECT BY level <= (sysdate - To_Date('16-09-12','DD-MM-RR')) * 24 ;
  26.  
  27. SELECT SUM(num_apps) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour
  28. FROM(
  29. SELECT count(*) as num_apps, created_ts
  30. FROM accounts
  31. WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-09-12','DD-MM-RR')
  32. UNION ALL
  33. select 0 as num_apps, (To_Date('16-09-12','DD-MM-RR') + level / 24) as created_ts
  34. FROM dual
  35. CONNECT BY level <= (sysdate - To_Date('16-09-12','DD-MM-RR')) * 24 ;
  36. )
  37. GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24')
  38. ORDER BY app_date, app_hour
  39. ;
  40.  
  41. create table dbo.sequence
  42. (
  43. id int not null primary key clustered ,
  44. )
  45.  
  46. create table dbo.SomeTable
  47. (
  48. account_id int not null primary key clustered ,
  49. date_created date not null ,
  50. time_created time not null ,
  51. )
  52.  
  53. select year_created = years.id ,
  54. month_created = months.id ,
  55. day_created = days.id ,
  56. hour_created = hours.id ,
  57. volume = t.volume
  58. from ( select * ,
  59. is_leap_year = case
  60. when id % 400 = 0 then 1
  61. when id % 100 = 0 then 0
  62. when id % 4 = 0 then 1
  63. else 0
  64. end
  65. from dbo.sequence
  66. where id between 1980 and year(current_timestamp)
  67. ) years
  68. cross join ( select *
  69. from dbo.sequence
  70. where id between 1 and 12
  71. ) months
  72. left join ( select *
  73. from dbo.sequence
  74. where id between 1 and 31
  75. ) days on days.id <= case months.id
  76. when 2 then 28 + years.is_leap_year
  77. when 4 then 30
  78. when 6 then 30
  79. when 9 then 30
  80. when 11 then 30
  81. else 31
  82. end
  83. cross join ( select *
  84. from dbo.sequence
  85. where id between 0 and 23
  86. ) hours
  87. left join ( select date_created ,
  88. hour_created = datepart(hour,time_created ) ,
  89. volume = count(*)
  90. from dbo.SomeTable
  91. group by date_created ,
  92. datepart(hour,time_created)
  93. ) t on datepart( year , t.date_created ) = years.id
  94. and datepart( month , t.date_created ) = months.id
  95. and datepart( day , t.date_created ) = days.id
  96. and t.hour_created = hours.id
  97. order by 1,2,3,4
  98.  
  99. SELECT count(*) as num_apps, to_char(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour
  100. FROM (select level-1 as hour FROM Dual CONNECT BY level <= 24) h
  101. LEFT JOIN accounts a on h.hour = to_number(to_char(a.created_ts,'HH24'))
  102. WHERE created_ts >= To_Date('16-Aug-12','DD-Mon-RR')
  103. GROUP BY trunc(created_ts), h.hour
  104. ORDER BY app_date, app_hour
Add Comment
Please, Sign In to add comment