根据不同的列值的滤波行上的滤波行

发布于 2025-01-31 17:42:52 字数 3764 浏览 2 评论 0原文

我一直在尝试根据task_dts的值过滤带有代码的一定值行。 本质上,我只需要行 per id ,其中task_dts timestamp属于code_dts当前 code> code 和code_dts code

例如;对于代码等于'z'的行,我只想要task_dtscode> code_dts的范围内的行,值'z '和价值'y'。 对于code等于'y'的行,我只想要task_dtscode> code_dts for Value'的范围内的行。 y'and value x'等

桌子看起来如下:

rowIDID代码code_dtstask_dts2022-02-02-01
1Az2022-02-01 10:17:08.40300021:21:
27.00000021我的10 -01 10:17:08.4030002:21:31.000000
3AZ2022-02-01 10:1732022-02-02-01 12:41:41:
42022-02-01 1043.000000: 08.403000 11:12:13.27000012022-02-01 10:21:21:27.000000
5AY2022-02-01 11:12:13.27000032022-02-02-01 12:41:43.000000
6AY2022-02-02-01 11: 12:13.27000082022-02-21 14:57:53.000000
7AX2022-02-21 12:28:50.64700062022-02-21 14:57:53.000000
8Ax2022-21 12:28: 50.64700072022-02-21 14:57:54.000000
9bh2022-04-05 13:44:16.03000012022-04-05 14:03:56.570000
10bh2022-04-05 13:44:16.03000022022-04-05 14:03:56.570000
11BI2022-04-06 13:44:16.03000012022-04-04-05 14:03:
56.570000 12BJ20222-2022-2022-04-07 13:44:16.030000320222- 04-05 14:03:56.570000

输出看起来像:

ROWIDIDIDCODE_DTS任务task_dts
1AZ2022-02-02-01 10:17:08.40300012022-02-02-01 10:21:21:27.000000
2AZ2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-2022 A z 2022-2022-2022-2022-2022-2022-2022-2022-2022- 2022 A Z 2022-- 2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-21:21:21:21:27.0000 02-01 10:17:08.40300022022-02-01 10:21:31.000000
5ay2022-02-01 11:12:13.27000032022-02-01 12:41:43.000000
7ax2022-02- 21 12:28:50.64700062022-02-21 14:57:53.000000
8Ax2022-02-21 12:28:50.647000 72022-02-21 14:57:54.000000
10BH2022-04-04-04-05 13 :44:16.0300002:56.570000
BI2022-04-0644:16.030000 12022-04-04-04:03:03:03:56.570000
122022-04-05 14:03: 031113 : :16.03000032022-04-05 14:03:56.570000

我试图以合格而无益地解决此问题。帮助您将不胜感激

I've been trying to filter rows with a certain value for code based on the values of task_dts.
Essentially I only want the rows per id where the task_dts timestamp falls between the code_dts timestamp of the current code and the code_dts of the following code.

For example; for the rows where code equals 'z', I only want the rows where the task_dts is in the range of the code_dts for value 'z' and value 'y'.
For the rows where the code equals 'y', I only want the rows where the task_dts is in the range of the code_dts for value 'y' and value 'x' etc

My table looks the following:

rowididcodecode_dtstasktask_dts
1az2022-02-01 10:17:08.40300012022-02-01 10:21:27.000000
2az2022-02-01 10:17:08.40300022022-02-01 10:21:31.000000
3az2022-02-01 10:17:08.40300032022-02-01 12:41:43.000000
4ay2022-02-01 11:12:13.27000012022-02-01 10:21:27.000000
5ay2022-02-01 11:12:13.27000032022-02-01 12:41:43.000000
6ay2022-02-01 11:12:13.27000082022-02-21 14:57:53.000000
7ax2022-02-21 12:28:50.64700062022-02-21 14:57:53.000000
8ax2022-02-21 12:28:50.64700072022-02-21 14:57:54.000000
9bh2022-04-05 13:44:16.03000012022-04-05 14:03:56.570000
10bh2022-04-05 13:44:16.03000022022-04-05 14:03:56.570000
11bi2022-04-06 13:44:16.03000012022-04-05 14:03:56.570000
12bj2022-04-07 13:44:16.03000032022-04-05 14:03:56.570000

The output would look like this:

rowididcodecode_dtstasktask_dts
1az2022-02-01 10:17:08.40300012022-02-01 10:21:27.000000
2az2022-02-01 10:17:08.40300022022-02-01 10:21:31.000000
5ay2022-02-01 11:12:13.27000032022-02-01 12:41:43.000000
7ax2022-02-21 12:28:50.64700062022-02-21 14:57:53.000000
8ax2022-02-21 12:28:50.64700072022-02-21 14:57:54.000000
10bh2022-04-05 13:44:16.03000022022-04-05 14:03:56.570000
11bi2022-04-06 13:44:16.03000012022-04-05 14:03:56.570000
12bj2022-04-07 13:44:16.03000032022-04-05 14:03:56.570000

I've tried to solve this with qualify without succes. help would be much appreciated

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

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

发布评论

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

评论(2

離殇 2025-02-07 17:42:52

您可以使用表格表达式来预测时间戳范围。然后,过滤很容易。

例如:

select t.*
from t
join (
  select code, dt, lead(dt) over(order by dt) as next_dt
  from (select code, min(code_dts) as dt from t group by code) x
) y on t.code = y.code
where t.task_dts between y.dt and y.next_dt or y.next_dt is null

You can use a table expression to pre-compute the timestamp ranges. Then, filtering is easy.

For example:

select t.*
from t
join (
  select code, dt, lead(dt) over(order by dt) as next_dt
  from (select code, min(code_dts) as dt from t group by code) x
) y on t.code = y.code
where t.task_dts between y.dt and y.next_dt or y.next_dt is null
梦年海沫深 2025-02-07 17:42:52

阅读了Impaler的答案后,我终于理解了您的要求:-)

这是基于窗口功能的逻辑相同的:

with cte as
 ( 
   select t.*
     -- next code_dts, i.e. at least one row will return 
     -- the code_dts of the following code 
     ,lead(code_dts,1,task_dts) over (order by code_dts) as next_dts
   from tab as t
 )
select *
from cte
qualify task_dts between code_dts
                     -- assign the next code's dts to all rows within the same code
                     and max(next_dts) over (partition by code)
; 

很难分辨哪个会更好...

After reading The Impaler's answer I finally understood your requirements :-)

This is the same logic based on Window Functions:

with cte as
 ( 
   select t.*
     -- next code_dts, i.e. at least one row will return 
     -- the code_dts of the following code 
     ,lead(code_dts,1,task_dts) over (order by code_dts) as next_dts
   from tab as t
 )
select *
from cte
qualify task_dts between code_dts
                     -- assign the next code's dts to all rows within the same code
                     and max(next_dts) over (partition by code)
; 

Hard to tell which one will perfrom better...

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