如何计算 TSQL 中特定值之间的差异的平均值?
嘿伙计们,抱歉,这是一个有点长的问题...
我有一个包含以下列的表格:
[ChatID] [User] [LogID] [CreatedOn] [Text]
我需要找到的是平均响应时间给定的用户 ID 到另一个特定的用户 ID。因此,如果我的数据如下所示:
[1] [john] [20] [1/1/11 3:00:00] [Hello]
[1] [john] [21] [1/1/11 3:00:23] [Anyone there?]
[1] [susan] [22] [1/1/11 3:00:43] [Hello!]
[1] [susan] [23] [1/1/11 3:00:53] [What's up?]
[1] [john] [24] [1/1/11 3:01:02] [Not much]
[1] [susan] [25] [1/1/11 3:01:08] [Cool]
...那么我需要看到 Susan 的平均响应时间为 (20 + 6) / 2 =>约翰13秒,约翰平均为(9 / 1) =>苏珊还有 9 秒。
我什至不确定这可以在基于集合的逻辑中完成,但如果有人有任何想法,他们将不胜感激!
Hey folks, sorry this is a bit of a longer question...
I have a table with the following columns:
[ChatID] [User] [LogID] [CreatedOn] [Text]
What I need to find is the average response time for a given user id, to another specific user id. So, if my data looks like:
[1] [john] [20] [1/1/11 3:00:00] [Hello]
[1] [john] [21] [1/1/11 3:00:23] [Anyone there?]
[1] [susan] [22] [1/1/11 3:00:43] [Hello!]
[1] [susan] [23] [1/1/11 3:00:53] [What's up?]
[1] [john] [24] [1/1/11 3:01:02] [Not much]
[1] [susan] [25] [1/1/11 3:01:08] [Cool]
...then I need to see that Susan has an average response time of (20 + 6) / 2 => 13 seconds to John, and John has an average of (9 / 1) => 9 seconds to Susan.
I'm not even sure this can be done in set-based logic, but if anyone has any ideas, they'd be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我没有一台电脑来验证语法或任何东西,但我认为这应该给你一个起点:
本质上,这个想法是将行号添加到数据中,这样你就可以将一行连接到下一行(所以你有声明后立即作出回应)。连接行后,您可以获得两个条目之间经过的时间,然后按 ChatID(我假设单独聊天之间的时间不相关)和两个用户对数据进行分组。就像我说的,这只是一个起点,因为我确信我的查询中可能存在一些额外的条件和/或错误:)
I don't have a PC to verify syntax or anything, but I think this should give you a starting place:
Essentially, the idea is to add row numbers to the data so you can join one row to the next row (so you have a statement followed by its immediate response). Once you have the rows joined, you can get the time elapsed between the two entries, and then group the data by the ChatID (I'm assuming that times between separate chats aren't relevant) and the two users. Like I said though, this is just a starting place as I'm sure there may be some additional criteria and/or bugs in my query :)
在进入光标之前尝试如下简单的操作。
这个可以工作并且不涉及使用游标。如果我有更多时间,我什至可以消除临时表,但是嘿......它有效。
Try something simple like the following before moving into cursors.
This one works and doesn't involve using cursors. If I had more time, I could probably even eliminate the temp table, but hey... it works.
似乎您需要一个光标来逐步浏览每一行并检查记录中用户的更改,并获取该时间的差异,并将其存储在某个地方(可能是临时表),并稍后聚合它。
我相信它可以在 TSQL 中完成,逻辑如下:
我相信您可以弄清楚如何声明所有参数。
Seems like you need a cursor to step through each line and check for the change of user in the record, and get the difference of that time, and store it somewhere(temp table maybe), and aggregate it later.
I believe it can be done in TSQL, logic would be something like:
I'm sure you can figure out how to declare all the parameters.
这可以通过
RowNumber()
来完成和DateDiff()
This can be done with
RowNumber()
andDateDiff()
试试这个:
示例数据:
输出:
分组:
输出:
分组结果:
输出:
聊天和响应:
输出:
聊天的响应平均时间:
输出:
将在 Postgresql 上开箱即用。要使其在 Sql Server 上工作,只需将
response_created_on - returned_to_timestamp
更改为相应的 Sql Server DATEDIFF 构造(我记不起 DATEDIFF 秒数)Try this:
Sample data:
Output:
The grouping:
Output:
The grouped result:
Output:
Chat and responses:
Output:
Chat's response average time:
Output:
Will work out-of-the box on Postgresql. To make it work on Sql Server, just change the
response_created_on - responded_to_timestamp
to corresponding Sql Server DATEDIFF construct (i cannot recall off the top my head what's the DATEDIFF for seconds)这将完成工作,但我不确定它将如何扩展:
选择了 4 条记录 [Fetch MetaData: 0ms] [Fetch Data: 0ms]
(chat_id, log_id) 上的索引应该没问题。
如果您想消除相同的响应,您只需要在外部 where 子句中使用 != 即可:
This will get the job done, but I'm not sure how it will scale:
4 record(s) selected [Fetch MetaData: 0ms] [Fetch Data: 0ms]
It should be okay with an index on (chat_id, log_id).
If you'd like to eliminate same responses, all you need is a != in the outer where clause: