gpt4 book ai didi

sql - 计算两个日期之间事件的记录数

转载 作者:行者123 更新时间:2023-12-03 02:33:41 24 4
gpt4 key购买 nike

我正在尝试执行查询来检索给定时间内的事件调用数,

我有一个表 CALLS,其中包含带有 AnsweredTime 和 ReleaseTime 的所有调用记录,我需要检索当天每秒的事件调用数。

所以我做了以下工作:

    SELECT dt,
(SELECT COUNT(Id) FROM Calls WHERE
AnsweredTimestamp <= dt AND
ReleasedTimestamp >= dt)
FROM CALView
WHERE dt >='2011-10-05'
and dt <'2011-10-06'

CalView 是一个包含两个日期之间每一秒的表格

查询返回此(部分):

2011-10-05 09:40:00.000 20
2011-10-05 09:40:01.000 20
2011-10-05 09:40:02.000 20
2011-10-05 09:40:03.000 21
2011-10-05 09:40:04.000 21
2011-10-05 09:40:05.000 21
2011-10-05 09:40:06.000 21
2011-10-05 09:40:07.000 21
2011-10-05 09:40:08.000 21
2011-10-05 09:40:09.000 21
2011-10-05 09:40:10.000 20
2011-10-05 09:40:11.000 20
2011-10-05 09:40:12.000 19
2011-10-05 09:40:13.000 19
2011-10-05 09:40:14.000 19
2011-10-05 09:40:15.000 19
2011-10-05 09:40:16.000 19
2011-10-05 09:40:17.000 19
2011-10-05 09:40:18.000 19
2011-10-05 09:40:19.000 19
2011-10-05 09:40:20.000 18
2011-10-05 09:40:21.000 18
2011-10-05 09:40:22.000 18
2011-10-05 09:40:23.000 18
2011-10-05 09:40:24.000 18
2011-10-05 09:40:25.000 18
2011-10-05 09:40:26.000 18
2011-10-05 09:40:27.000 18
2011-10-05 09:40:28.000 18
2011-10-05 09:40:29.000 18
2011-10-05 09:40:30.000 18
2011-10-05 09:40:31.000 18
2011-10-05 09:40:32.000 18
2011-10-05 09:40:33.000 18
2011-10-05 09:40:34.000 18
2011-10-05 09:40:35.000 19
2011-10-05 09:40:36.000 18
2011-10-05 09:40:37.000 18
2011-10-05 09:40:38.000 18
2011-10-05 09:40:39.000 18
2011-10-05 09:40:40.000 18
2011-10-05 09:40:41.000 18
2011-10-05 09:40:42.000 18
2011-10-05 09:40:43.000 18
2011-10-05 09:40:44.000 19
2011-10-05 09:40:45.000 19
2011-10-05 09:40:46.000 19
2011-10-05 09:40:47.000 19
2011-10-05 09:40:48.000 19
2011-10-05 09:40:49.000 19
2011-10-05 09:40:50.000 19
2011-10-05 09:40:51.000 19
2011-10-05 09:40:52.000 19
2011-10-05 09:40:53.000 19
2011-10-05 09:40:54.000 19
2011-10-05 09:40:55.000 19
2011-10-05 09:40:56.000 19
2011-10-05 09:40:57.000 19
2011-10-05 09:40:58.000 20
2011-10-05 09:40:59.000 20
2011-10-05 09:41:00.000 20
2011-10-05 09:41:01.000 20
2011-10-05 09:41:02.000 20
2011-10-05 09:41:03.000 20
2011-10-05 09:41:04.000 20
2011-10-05 09:41:05.000 20
2011-10-05 09:41:06.000 20
2011-10-05 09:41:07.000 20
2011-10-05 09:41:08.000 20
2011-10-05 09:41:09.000 19
2011-10-05 09:41:10.000 19
2011-10-05 09:41:11.000 19
2011-10-05 09:41:12.000 19
2011-10-05 09:41:13.000 19
2011-10-05 09:41:14.000 19
2011-10-05 09:41:15.000 19
2011-10-05 09:41:16.000 20
2011-10-05 09:41:17.000 20
2011-10-05 09:41:18.000 20
2011-10-05 09:41:19.000 20
2011-10-05 09:41:20.000 20
2011-10-05 09:41:21.000 20
2011-10-05 09:41:22.000 20
2011-10-05 09:41:23.000 20
2011-10-05 09:41:24.000 20
2011-10-05 09:41:25.000 20
2011-10-05 09:41:26.000 20
2011-10-05 09:41:27.000 20
2011-10-05 09:41:28.000 20
2011-10-05 09:41:29.000 20
2011-10-05 09:41:30.000 19
2011-10-05 09:41:31.000 19
2011-10-05 09:41:32.000 19
2011-10-05 09:41:33.000 19
2011-10-05 09:41:34.000 20
2011-10-05 09:41:35.000 20
2011-10-05 09:41:36.000 20
2011-10-05 09:41:37.000 19
2011-10-05 09:41:38.000 19
2011-10-05 09:41:39.000 19
2011-10-05 09:41:40.000 19
2011-10-05 09:41:41.000 19
2011-10-05 09:41:42.000 19
2011-10-05 09:41:43.000 19
2011-10-05 09:41:44.000 19
2011-10-05 09:41:45.000 19
2011-10-05 09:41:46.000 18
2011-10-05 09:41:47.000 18
2011-10-05 09:41:48.000 19
2011-10-05 09:41:49.000 20
2011-10-05 09:41:50.000 20
2011-10-05 09:41:51.000 20
2011-10-05 09:41:52.000 20
2011-10-05 09:41:53.000 20

所以我得到了很好的结果,但是查询非常慢,我已经完成了索引,我只是想知道是否还有更多我可以做的事情..

最佳答案

您将在此处创建一个有趣的笛卡尔坐标。

Select dt,count(1)
from calview
left join calls on calls.AnsweredTimestamp <= calview.dt AND
calls.ReleasedTimestamp >= calview.dt
WHERE dt >='2011-10-05'
and dt <'2011-10-06'
group by dt

我相信这应该为您在调用事件的每一秒提供一行......然后这是一个简单的计数。希望我的语法是正确的,我没有 ms sql server 环境来确认。

预计到达时间:使用了左连接,因此任何有 0 次调用的秒数都将显示在此处,且计数为空。如果切换到内部联接,该行将在 1 秒间隔内消失。

关于sql - 计算两个日期之间事件的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8261872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com