Oracle 事件计数查询
我的 SAMPLE
表具有以下五列:
sample_id (PK) (NUMBER)
sampled_on (DATE)
received_on (DATE)
completed_on (DATE)
authorized_on (DATE)
我想要一个每小时一行(受给定日期范围限制)和五列的查询:
- 小时
YYYY-MM-DD HH24< /code>
- 该小时内采样的样品数量
- 该小时内收到的样品数量 该小时
- 内完成的样品数量
- 该小时内授权的样品数量
请提供查询或至少提供正确方向的一点。
以赏金重新开放:
+300 声誉,是第一个将 Rob van Wijk 的答案(对示例的单次访问)合并到我可以按日期范围高效查询的视图中的人(start_date/end_date
或 start_date/num_days
)。
My SAMPLE
table has the following five columns:
sample_id (PK) (NUMBER)
sampled_on (DATE)
received_on (DATE)
completed_on (DATE)
authorized_on (DATE)
I would like a query with one row per hour (constrained by a given date range) and five columns:
- The hour
YYYY-MM-DD HH24
- Number of samples sampled during that hour
- Number of samples received during that hour
- Number of samples completed during that hour
- Number of samples authorized during that hour
Please provide a query or at least a point in the right direction.
Reopened with bounty:
+300 reputation for the first person to incorporate Rob van Wijk's answer (single access to sample) into a view where I can efficiently query by date range (start_date/end_date
or start_date/num_days
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这可能不是最漂亮或最优化的解决方案,但它似乎有效。说明:首先将所有日期转换为YYYY-MM-DD HH24格式,然后按日期+HH24收集采样/接收/完成/授权的数量,最后合并在一起。
这假设表“样本”创建了如下内容:
This may not be the prettiest or most optimal solution, but it seems to work. Explanation: first convert all the dates to YYYY-MM-DD HH24 format, next gather number sampled/received/completed/authorized by date+HH24, finally join together.
This assumes a table "sample" created something like this:
这是一个例子。首先创建表并插入一些随机数据。
然后引入给定日期范围的变量并填充它们。
首先,您必须生成给定日期范围内的所有小时。这可以确保,如果您有一小时没有日期,您仍然会拥有包含 4 个零的记录。实现在 all_hours 查询中。查询的其余部分(只有一个表访问示例表!)可以像这样非常简单。
希望这有帮助。
问候,
抢。
Here is an example. First create the table and insert some random data.
Then introduce the variables for your given date range and fill them.
First you'll have to generate all hours in your given date range. This makes sure that in case you have an hour where no dates are present, you'll still have a record with 4 zeros. The implementation is in the all_hours query. The rest of the query (with only one table access to your sample table!) can then be quite simple like this.
Hope this helps.
Regards,
Rob.
我会做 4 个这样的查询(每个日期一个):
然后将数据放在应用程序中。如果您确实想要单个查询,您可以在
小时
上加入各个查询。I'd do are 4 queries like this (one for each date):
And then put the data together in the application. If you really want a single query, you can join the individual queries on
hour
.试试这个...
Try this...
也许像创建这个视图:
然后从视图中选择:
Maybe somthing like creating this view:
and then selecting from the view:
我现在建议:
为了查看视图是否确实只访问一次:
这会导致:
I now propose:
In order to see if the view is indeed accessed only once:
Which results in:
这就是我的想法,但我不确定它对于视图来说是否足够最佳。
然后,要查询,您可以使用正常的日期结构进行查询:
编辑
好的,所以我创建了一个示例表并做了一些指标:
解释计划是:
在我的机器上,过去 24 小时针对此视图的查询完成23 毫秒。不错,但只有 1,000 行。在考虑 4 个单独的查询之前,您需要对各个解决方案进行性能分析。
Here's what I'm thinking, but I'm not sure it's optimal enough for a view.
Then, to query, you could just query with normal date contructs:
EDIT
Okay, so I created a sample table and did some metrics:
The explain plan is:
On my machine, the a query against this view for the past 24 hours completes in 23ms. Not bad, but it's only 1,000 rows. Before you discount the 4 separate queries, you'll need to do performance analysis of the individual solutions.
与 René Nyffenegger 的想法类似。按每种类型的日期字段进行筛选,然后合并计数。
请注意,不可能在一个 Select 中执行此查询,因为您需要对每个日期字段进行分组和排序,如果不拆分为单独的子查询,这是不可能的。
对于此示例,我已将日期范围编码为“2009-11-04”到“2009-11-04 23:59:59”:
Similar to René Nyffenegger's idea. Filter by each type of date field, and then amalgamate the counts.
Note, that it's not possible to do this query in one Select, because you need to both Group and Order By each date field, this is impossible without splitting into separate sub-queries.
I have coded a date range of '2009-11-04' to '2009-11-04 23:59:59' for this example:
尝试:
不使用子查询分解:
查询访问 SAMPLES 表两次 - 第一次获取最早的数据用于构建
date_by_hour
值的最新日期。Try:
Without using Subquery Factoring:
The query accesses the SAMPLES table twice - the first time to get the earliest & latest date to frame the construction of the
date_by_hour
value.