简体   繁体   中英

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.

This is an example of how my datase looks like:

year month day event_timestamp server_timestamp
2021 07 01 2021-01-19 01:48:20.000 2021-07-01 01:48:20.000
2022 04 09 2022-04-08 01:48:20.000 2022-04-09 01:48:20.000
2023 01 19 2023-01-08 01:48:20.000 2023-01-19 01:48:20.000
2022 02 21 2022-01-09 01:48:20.000 2022-02-21 01:48:20.000
2021 08 05 2021-01-23 01:48:20.000 2021-08-05 01:48:20.000

What is the best way to solve this using 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?!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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