Oracle/SQL 中的时间切片

发布于 2024-07-09 20:01:37 字数 316 浏览 7 评论 0原文

我有一个相当大的 Oracle 表,其中包含代表工作单元的行,除了其他元数据之外,还包含开始时间和结束时间的列。

我需要根据一些任意的过滤标准和报告时间段从这些数据生成使用情况图表。 例如,向我展示从上周二上午 7:00 开始的 24 小时内 Alice 的所有工作的图表。 每个数据库行将在图表中垂直堆叠。

我可以使用高级语言来完成此操作,方法是查询所有可能相关的行,将每个行的时间分割为 1 分钟的存储桶,然后绘制结果图。 但是有没有一种有效的方法来在 SQL 中进行时间切片呢? 或者现有的 Oracle 技术可以做到这一点吗?

谢谢!

I have a large-ish Oracle table containing rows representing units of work, with columns for start time and end time in addition to other meta-data.

I need to generate usage graphs from this data, given some arbitrary filtering criteria and a reporting time period. E.g., show me a graph of all of Alice's jobs for the 24-hour period starting last Tuesday at 7:00am. Each DB row will stack vertically in the graph.

I could do this in a high-level language by querying all potentially relevant rows, time slicing each one into 1-minute buckets, and graphing the result. But is there an efficient way to do this time slicing in SQL? Or is there an existing Oracle technology that does this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2024-07-16 20:01:37

在获取数据方面,您可以使用“group by”和“truncate' 将数据分割为 1 分钟间隔。 例如:

SELECT user_name, truncate(event_time, 'YYYYMMDD HH24MI'), count(*)
FROM job_table
WHERE event_time > TO_DATE( some start date time)
AND user_name IN ( list of users to query )
GROUP BY user_name, truncate(event_time, 'YYYYMMDD HH24MI') 

这将为您提供如下结果(假设 8.00 和 8.01 之间有 20 行 alice,8.01 和 8.02 之间有 40 行):

Alice  2008-12-16 08:00   20
Alice  2008-12-16 08:01   40

In terms of getting the data out, you can use 'group by' and 'truncate' to slice the data into 1 minute intervals. eg:

SELECT user_name, truncate(event_time, 'YYYYMMDD HH24MI'), count(*)
FROM job_table
WHERE event_time > TO_DATE( some start date time)
AND user_name IN ( list of users to query )
GROUP BY user_name, truncate(event_time, 'YYYYMMDD HH24MI') 

This will give you results like below (assuming there are 20 rows for alice between 8.00 and 8.01 and 40 rows between 8.01 and 8.02):

Alice  2008-12-16 08:00   20
Alice  2008-12-16 08:01   40
冷了相思 2024-07-16 20:01:37

最好的选择是拥有一张表(如果时间片是动态的,则动态生成的临时表就可以了),然后加入该表。

Your best bet is to have a table (a temporary one generated on the fly would be fine if the time-slice is dynamic) and then join against that.

初见 2024-07-16 20:01:37

这应该做得相当好。 它将细分到分钟(一天的 1/1440)。

SELECT 
  to_char(Times.time,'hh24:mi'),
  count(*)   
FROM   
  (SELECT
     time
   FROM  
     dual
   WHERE 
     1=2 
   MODEL 
     dimension by ( 0 as key )
     measures     ( sysdate -1 as time )
     rules upsert ( time[ for key from 0 to 1 increment (1/1440) ] = sysdate-1 + cv(key)) ) Times,
  job_table
WHERE 
  begintime <= Times.time 
AND 
  endtime > Times.time 
AND
  user_name = 'Alice'
GROUP BY 
  Times.time 
ORDER BY
  Times.time

我确信有一种更快的方法可以做到这一点,但这是我能想到的最好的方法。
希望这可以帮助。

This should do it fairly well. It will breakdown to the minute (1/1440 of a day).

SELECT 
  to_char(Times.time,'hh24:mi'),
  count(*)   
FROM   
  (SELECT
     time
   FROM  
     dual
   WHERE 
     1=2 
   MODEL 
     dimension by ( 0 as key )
     measures     ( sysdate -1 as time )
     rules upsert ( time[ for key from 0 to 1 increment (1/1440) ] = sysdate-1 + cv(key)) ) Times,
  job_table
WHERE 
  begintime <= Times.time 
AND 
  endtime > Times.time 
AND
  user_name = 'Alice'
GROUP BY 
  Times.time 
ORDER BY
  Times.time

I'm sure there is a faster way to do this, but this is the best I can come up with.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文