当sqlite中完全显示出野外值时,我将如何确定野外值出现的速率?

发布于 2025-01-30 18:18:00 字数 604 浏览 1 评论 0原文

我有一个巨大的数据集。每个记录上都有一个时间戳(以字符串格式)。我正在尝试确定特定字段是一个特定值的次数。以一种更具体的方式:

+---------------------+-------+
| TimeStamp           | Value |
+---------------------+-------+
| 2022-01-01 00:00:00 | A     | A shows up 3 times in the first hour
| 2022-01-01 00:00:01 | B     |
| 2022-01-01 00:00:05 | A     |
| 2022-01-01 00:00:09 | A     |
| 2022-01-01 05:00:01 | A     | A shows up 2 times in the sixth hour
| 2022-01-01 05:00:02 | A     |
+---------------------+-------+

我想看看一个小时内a平均每小时出现多少次,不包括根本没有出现的时间。在这种情况下,5/2 =>每小时2.5次。 SQL是否可以做到这一点,还是我必须编写一个外部脚本来执行此操作?

I have a huge data set. Each record has a timestamp on it (in string format). I'm trying to determine how many times a particular field is a particular value. In a more concrete fashion:

+---------------------+-------+
| TimeStamp           | Value |
+---------------------+-------+
| 2022-01-01 00:00:00 | A     | A shows up 3 times in the first hour
| 2022-01-01 00:00:01 | B     |
| 2022-01-01 00:00:05 | A     |
| 2022-01-01 00:00:09 | A     |
| 2022-01-01 05:00:01 | A     | A shows up 2 times in the sixth hour
| 2022-01-01 05:00:02 | A     |
+---------------------+-------+

I'd like to see how many times in an hour that A shows up on average hourly excluding the time that it doesn't show up at all. In this case 5/2 => 2.5 times per hour. Is this even possible with SQL or will I have to write an external script to do this?

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

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

发布评论

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

评论(1

眼泪淡了忧伤 2025-02-06 18:18:00

您需要2个聚合级别:

SELECT AVG(counter) average
FROM (
  SELECT COUNT(*) counter
  FROM tablename
  WHERE Value = 'A'
  GROUP BY strftime('%Y-%m-%d %H', TimeStamp)
);

或,使用avg()窗口函数:

SELECT DISTINCT AVG(COUNT(*)) OVER () average
FROM tablename
WHERE Value = 'A'
GROUP BY strftime('%Y-%m-%d %H', TimeStamp);

如果您想要所有value s:

SELECT Value, AVG(counter) average
FROM (
  SELECT Value, COUNT(*) counter
  FROM tablename
  GROUP BY Value, strftime('%Y-%m-%d %H', TimeStamp)
)
GROUP BY Value;

或:

SELECT DISTINCT Value, 
       AVG(COUNT(*)) OVER (PARTITION BY Value) average
FROM tablename
GROUP BY Value, strftime('%Y-%m-%d %H', TimeStamp);

请参阅 demo

You need 2 levels of aggregation:

SELECT AVG(counter) average
FROM (
  SELECT COUNT(*) counter
  FROM tablename
  WHERE Value = 'A'
  GROUP BY strftime('%Y-%m-%d %H', TimeStamp)
);

or, with AVG() window function:

SELECT DISTINCT AVG(COUNT(*)) OVER () average
FROM tablename
WHERE Value = 'A'
GROUP BY strftime('%Y-%m-%d %H', TimeStamp);

If you want results for all Values:

SELECT Value, AVG(counter) average
FROM (
  SELECT Value, COUNT(*) counter
  FROM tablename
  GROUP BY Value, strftime('%Y-%m-%d %H', TimeStamp)
)
GROUP BY Value;

or:

SELECT DISTINCT Value, 
       AVG(COUNT(*)) OVER (PARTITION BY Value) average
FROM tablename
GROUP BY Value, strftime('%Y-%m-%d %H', TimeStamp);

See the demo.

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