SQL-如何在两个值之间计数行?

发布于 2025-02-04 05:38:12 字数 1298 浏览 3 评论 0原文

我有一个SQL表,其中包含时间戳和相关的值。看起来像这样:

Original_table:

TIMESTAMP
2022-06-03 00:09:15.00075
:09:16.0000
2022-06-06-03 00:09:09:2022-06-03 00
19.000 :29.00012
2022-06-03 00:09:44.0000
2022-06-03 00:09:55.0005
2022-06-06-03 00:09:56.0000

我试图制作一个像这样的桌子包含在timestamp_start字段中的值== 0的行。在Timestamp_end字段中,它包含下一个值不等于0的时间戳记:

wanty_table:

timestamp_starttimestamp_end
2022-06-03 00:09:16.0002022-06-06-06-03 00:09:09:09:29.000
2022-06-06-06-06-06-06-06-06-06-06-06-06-06-06-06-06-06-03 00 :09:44.0002022-06-03 00:09:55.000
2022-06-06-03 00:09:56.000null

*** null ***编辑***最后一个,timestamp_end必须为空(null),因为之后有0 。

我尝试使用

coalesce(lead(timeStamp,SELECT COUNT(*) 
FROM ORIGINAL_TABLE
WHERE value=0

的行数吗

它仅计算值== 0有任何提示 ?谢谢!

I have an SQL table that contains timestamp and value associated. It looks like this :

ORIGINAL_TABLE:

timestampvalue
2022-06-03 00:09:15.00075
2022-06-03 00:09:16.0000
2022-06-03 00:09:19.0000
2022-06-03 00:09:29.00012
2022-06-03 00:09:44.0000
2022-06-03 00:09:55.0005
2022-06-03 00:09:56.0000

I am trying to make a table like this one, where it only contains the rows where value==0 in the timestamp_start field. In the timestamp_end field, it contains the timestamp of the next value that is not equal to 0:

WANTED_TABLE :

timestamp_starttimestamp_end
2022-06-03 00:09:16.0002022-06-03 00:09:29.000
2022-06-03 00:09:44.0002022-06-03 00:09:55.000
2022-06-03 00:09:56.000NULL

***EDIT *** For the last one, timestamp_end has to be empty (NULL) because there is 0 after.

I tried using

coalesce(lead(timeStamp,SELECT COUNT(*) 
FROM ORIGINAL_TABLE
WHERE value=0

However it only counts the number of rows where value == 0

Any tips ? Thanks!

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

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

发布评论

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

评论(1

北城挽邺 2025-02-11 05:38:12

我不知道您正在使用什么DB。但是我在PostgreSQL中写了此SQL查询,我认为此查询将在许多数据库中运行。

with org_table as materialized 
(
    select
        row_number() over (order by "timestamp") as r_num,
        "timestamp", 
        "value" 
    from original_table 
)
select min(a1.begintime) as begintime, a1.endtime from (
    select  
        t1."timestamp" as begintime, 
        min(t2."timestamp") as endtime
    from org_table t1 
    inner join org_table t2 on t2.r_num > t1.r_num and t2.value > 0
    where t1.value = 0 
    group by t1."timestamp"
) a1 
group by a1.endtime

结果:

begintime                   endtime
2022-06-03 00:09:16.000     2022-06-03 00:09:29.000
2022-06-03 00:09:44.000     2022-06-03 00:09:55.000

I don't know what DB you are using. But I wrote this SQL query in PostgreSQL, I think that this query will be run in many Databases.

with org_table as materialized 
(
    select
        row_number() over (order by "timestamp") as r_num,
        "timestamp", 
        "value" 
    from original_table 
)
select min(a1.begintime) as begintime, a1.endtime from (
    select  
        t1."timestamp" as begintime, 
        min(t2."timestamp") as endtime
    from org_table t1 
    inner join org_table t2 on t2.r_num > t1.r_num and t2.value > 0
    where t1.value = 0 
    group by t1."timestamp"
) a1 
group by a1.endtime

Result:

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