简体   繁体   English

如何遍历 SQL 中的分区,收集特定周的行并使用收集的行创建新表

[英]How to loop through partitions in SQL, collect rows from a specific week and create a new table with the collected rows

My data is organized in partitions.我的数据是按分区组织的。 Data is partitioned by the year, month, and day when the records were received by the servers.数据按服务器接收记录时的年、月和日进行分区。 The dataset contains a column with the timestamp that records when an event happened and another one with the timestamp of when the data corresponding was received in the servers.数据集包含一列记录事件发生时间的时间戳,另一列记录服务器接收相应数据的时间戳。

I need to go to each partition from 06/2021 to 06/2022, collect all rows that correspond to events that happened during the week of Jan. 18, 2021 to Jan. 24, 2021, and create a new table with the rows collected.我需要将 go 分配到从 06/2021 到 06/2022 的每个分区,收集与 2021 年 1 月 18 日到 2021 年 1 月 24 日这一周发生的事件对应的所有行,并创建一个包含收集到的行的新表.

This is an example of how my datase looks like:这是我的数据集的示例:

year month day event_timestamp事件时间戳 server_timestamp服务器时间戳
2021 2021年 07 07 01 01 2021-01-19 01:48:20.000 2021-01-19 01:48:20.000 2021-07-01 01:48:20.000 2021-07-01 01:48:20.000
2022 2022年 04 04 09 09 2022-04-08 01:48:20.000 2022-04-08 01:48:20.000 2022-04-09 01:48:20.000 2022-04-09 01:48:20.000
2023 2023年 01 01 19 19 2023-01-08 01:48:20.000 2023-01-08 01:48:20.000 2023-01-19 01:48:20.000 2023-01-19 01:48:20.000
2022 2022年 02 02 21 21 2022-01-09 01:48:20.000 2022-01-09 01:48:20.000 2022-02-21 01:48:20.000 2022-02-21 01:48:20.000
2021 2021年 08 08 05 05 2021-01-23 01:48:20.000 2021-01-23 01:48:20.000 2021-08-05 01:48:20.000 2021-08-05 01:48:20.000

What is the best way to solve this using SQL?使用 SQL 解决此问题的最佳方法是什么?

It seems like you do not need the columns year, month and day, since you have got the server_timestamp and you do not need a for loop?!似乎您不需要年、月和日列,因为您有 server_timestamp 并且不需要 for 循环?!

If i understood your question correctly, the answer could look something like that:如果我正确理解你的问题,答案可能是这样的:

create table new_table(
year int,
month nvarchr(2),
day nvarchar (2),
event_timestamp timestamp,
server_timestamp timestamp
)


select year, month, day, event_timestamp, server_timestamp
into new_table
from dataset
where server_timestamp >= 2021-06-01 00:00:00.000 
  and server_timestamp < 2022-07-01 00:00:00.000
  and event_timestamp >= 2021-01-18 00:00:00.000
  and event_timestamp < 2021-01-25 00:00:00.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM