如何在 T-SQL 中创建增量查询?
How can i create an incremental query in T-SQL?
我需要一些有用的示例来了解如何创建每周只引入增量数据的查询。将使用日期和时间列逐步提取此数据。
假设你有一个 table customer
create table customer(id int, name varchar(10), age int, created_on date, last_modified_on datetime)
create table customer_inc(id int, name varchar(10), age int, created_on date, last_modified_on datetime)
insert into customer(id,name,age,created_on,last_modified_on )
values(1,'a',54,'2022-01-01','2022-01-01 00:00:00.000')
values(2,'a',52,'2022-01-01','2022-01-01 00:00:00.000')
values(3,'a',53,'2022-01-01','2022-01-01 00:00:00.000')
id
name
age
created_on
last_modified_on
1
a
54
2022-01-01
2022-01-01 00:00:00.000
2
b
52
2022-01-01
2022-01-01 00:00:00.000
3
c
53
2022-01-01
2022-01-01 00:00:00.000
您将所有这些记录全部加载到您的 inc
table
现在更新记录 ID 3 并插入新记录
id
name
age
created_on
last_modified_on
1
a
54
2022-01-01
2022-01-01 00:00:00.000
2
b
52
2022-01-01
2022-01-01 00:00:00.000
3
f
53
2022-01-01
2022-01-27 00:00:00.000
2
y
30
2022-01-27
2022-01-27 00:00:00.000
现在您需要使用 last_modifed_on 通过从 customer_inc
中获取 last_modified_on 的最大值来识别这些新数据
Solution 1:
对以下数据集使用合并逻辑
select * from customer where last_modified_on > (select max(last_modified_on) from customer_inc)
Solution 2:
Always load the previous day(D-1) data as per your requirement.
对以下数据集使用合并逻辑
select * from customer where last_modified_on > cast(getdate()-1 as date)
我需要一些有用的示例来了解如何创建每周只引入增量数据的查询。将使用日期和时间列逐步提取此数据。
假设你有一个 table customer
create table customer(id int, name varchar(10), age int, created_on date, last_modified_on datetime)
create table customer_inc(id int, name varchar(10), age int, created_on date, last_modified_on datetime)
insert into customer(id,name,age,created_on,last_modified_on )
values(1,'a',54,'2022-01-01','2022-01-01 00:00:00.000')
values(2,'a',52,'2022-01-01','2022-01-01 00:00:00.000')
values(3,'a',53,'2022-01-01','2022-01-01 00:00:00.000')
id | name | age | created_on | last_modified_on |
---|---|---|---|---|
1 | a | 54 | 2022-01-01 | 2022-01-01 00:00:00.000 |
2 | b | 52 | 2022-01-01 | 2022-01-01 00:00:00.000 |
3 | c | 53 | 2022-01-01 | 2022-01-01 00:00:00.000 |
您将所有这些记录全部加载到您的 inc
table
现在更新记录 ID 3 并插入新记录
id | name | age | created_on | last_modified_on |
---|---|---|---|---|
1 | a | 54 | 2022-01-01 | 2022-01-01 00:00:00.000 |
2 | b | 52 | 2022-01-01 | 2022-01-01 00:00:00.000 |
3 | f | 53 | 2022-01-01 | 2022-01-27 00:00:00.000 |
2 | y | 30 | 2022-01-27 | 2022-01-27 00:00:00.000 |
现在您需要使用 last_modifed_on 通过从 customer_inc
中获取 last_modified_on 的最大值来识别这些新数据Solution 1:
对以下数据集使用合并逻辑
select * from customer where last_modified_on > (select max(last_modified_on) from customer_inc)
Solution 2: Always load the previous day(D-1) data as per your requirement.
对以下数据集使用合并逻辑
select * from customer where last_modified_on > cast(getdate()-1 as date)