SQL - 两个字段更改之间的最大和最短时间

发布于 2024-10-01 22:42:54 字数 554 浏览 6 评论 0原文

我有一个包含两列的 PL/SQL 表:log_date (DATE) 和 value (FLOAT)。数据非常细粒度,log_dates 之间的差异可能只有几毫秒。 随着时间的推移而变化。我想使用 SQL 查找 log_date 之间 value 增加所需的最大和最小时间量。

编辑:示例

log_date | value
-------------------  
  15:00  |  10
  15:01  |  10
  15:02  |  11
  15:03  |  11
  15:04  |  11
  15:05  |  11
  15:06  |  12

在 15:00 和 15:02 之间 value 增加,但在 15:03 到 15:06 之间也增加,这花费了更长的时间,所以我想要一个能够返回的查询(在本例中) “3 分钟”(作为日期或数字)- 增加所需的最长时间。

I have a PL/SQL table with two columns: log_date (DATE) and value (FLOAT). The data is very fine-grained, the difference between log_dates could be a few milliseconds. The value changes over time. I want to find, using SQL, the maximum and minimum amount of time between log_dates it takes for value to increase.

Edit: Example

log_date | value
-------------------  
  15:00  |  10
  15:01  |  10
  15:02  |  11
  15:03  |  11
  15:04  |  11
  15:05  |  11
  15:06  |  12

Between 15:00 and 15:02 value increased BUT it also increased between 15:03 and 15:06 which took longer, and so I want a query that would return (in this case) '3 minutes' (as a DATE or a NUMBER) - the longest amount of time it took for value to increase.

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

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

发布评论

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

评论(3

软甜啾 2024-10-08 22:42:54

我可以用 T-SQL 给你答案,但我不确定你使用的是什么方言。说实话,这里首先想到的是循环(其他人可能有更好的方法!):

DECLARE @temp TABLE ( log_date DATETIME, value FLOAT )
INSERT INTO @temp ( log_date, value ) SELECT log_date, value FROM <MyTableName>

DECLARE @diff TABLE ( time_diff INT, old_value FLOAT, new_value FLOAT )

-- the loop

DECLARE @prev_value FLOAT, 
        @cur_value FLOAT,
        @prev_log_date DATETIME,
        @cur_log_date DATETIME

WHILE EXISTS ( SELECT NULL FROM @temp )
BEGIN

    SELECT TOP 1 @cur_log_date = log_date, @cur_value = value
    FROM @temp
    ORDER BY log_date

    IF ( @prev_value IS NOT NULL AND @prev_log_date IS NOT NULL )
    BEGIN

        INSERT INTO @diff ( time_diff, old_value, new_value )
        SELECT DATEDIFF('ms', @prev_log_date, @cur_log_date ),
               @prev_value, @cur_value

    END

    SELECT @prev_log_date = @cur_log_date, @prev_value = @cur_value
    DELETE FROM @temp WHERE log_date = @cur_log_date

END

SELECT MAX(time_diff), MIN(time_diff) FROM @diffs

这样,您最终会得到一个包含所有差异的表,然后您可以查询该表。

华泰

I can give you an answer in T-SQL, but I'm not sure what dialect you're using. TBH, a loop here is the first thing that springs to mind (someone else may have a better way of doing it!):

DECLARE @temp TABLE ( log_date DATETIME, value FLOAT )
INSERT INTO @temp ( log_date, value ) SELECT log_date, value FROM <MyTableName>

DECLARE @diff TABLE ( time_diff INT, old_value FLOAT, new_value FLOAT )

-- the loop

DECLARE @prev_value FLOAT, 
        @cur_value FLOAT,
        @prev_log_date DATETIME,
        @cur_log_date DATETIME

WHILE EXISTS ( SELECT NULL FROM @temp )
BEGIN

    SELECT TOP 1 @cur_log_date = log_date, @cur_value = value
    FROM @temp
    ORDER BY log_date

    IF ( @prev_value IS NOT NULL AND @prev_log_date IS NOT NULL )
    BEGIN

        INSERT INTO @diff ( time_diff, old_value, new_value )
        SELECT DATEDIFF('ms', @prev_log_date, @cur_log_date ),
               @prev_value, @cur_value

    END

    SELECT @prev_log_date = @cur_log_date, @prev_value = @cur_value
    DELETE FROM @temp WHERE log_date = @cur_log_date

END

SELECT MAX(time_diff), MIN(time_diff) FROM @diffs

This way, you end up with a table of all differences that you can then query.

HTH

离笑几人歌 2024-10-08 22:42:54

您可以使用此查询查找特定值的最大和最小 log_date。但为此您必须指定该值。如果您想让查询更通用,您可能需要稍微修改一下查询

SELECT MAX(log_dates) AS MaxLogDate, MIN(log_dates) AS MinLogDate 
  FROM yourtable 
 WHERE <ANY condition IF needed> 
 GROUP 
    BY VALUE 
HAVING VALUE = <specify VALUE>;

You can use this query to find the max and min log_date for a specific value. But for this you will have to specify the value. You might have to modify the query a little if you want to make it more generic

SELECT MAX(log_dates) AS MaxLogDate, MIN(log_dates) AS MinLogDate 
  FROM yourtable 
 WHERE <ANY condition IF needed> 
 GROUP 
    BY VALUE 
HAVING VALUE = <specify VALUE>;
绅刃 2024-10-08 22:42:54

尝试如下操作:

select top 1 * from
(
  select 
    max(log_date) - min(log_date) as duration,
    value
  from logdata
  group by value
)
order by duration asc

并将其他值的 asc 更改为 desc。

[编辑]
我目前无法实际测试这一点,所以我不确定 max-min 是否有效,如果失败,您可以使用其他答案之一中发布的 datediff 函数作为替代方案。
[/编辑]

Try something like the following:

select top 1 * from
(
  select 
    max(log_date) - min(log_date) as duration,
    value
  from logdata
  group by value
)
order by duration asc

and change the asc to desc for the other value.

[edit]
I can't actually test this at the moment, so I'm not sure if the max-min will work, you can use the datediff function posted in one of the other answers as an alternative if this fails.
[/edit]

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