将数据插入 Table 使用条件
Insert Data Into a Table Using Condition
我有这两个table
daily_info
date
id
name
place
2022-04-11
1
Jhon
null
2022-04-12
1
Jhon
null
2022-04-13
1
Jhon
null
2022-04-14
1
Jhon
null
2022-04-15
1
Jhon
null
2022-04-16
1
Jhon
null
change_values
date
id
change_col
new_value
2022-04-14
1
place
Chicago
2022-04-16
1
place
India
2022-04-16
1
name
John watts
从这两个 table 中,我想创建一个新的 table,看起来像这样
history_table
date
id
name
place
2022-04-11
1
Jhon
null
2022-04-12
1
Jhon
null
2022-04-13
1
Jhon
null
2022-04-14
1
Jhon
Chicago
2022-04-15
1
Jhon
Chicago
2022-04-16
1
Jhon Watts
India
到目前为止我只能加入这两个 tables 并尝试使用 case-when 子句,我知道我需要使用滞后但我不是确定如何
我的代码
Select id,
date,
(Case When change_col = 'place' Then new_value End) As place,
(Case When change_col = 'name' Then new_value End) As name
From daily_info
Left Join change_values On d.id = c.id And d.date = c.date
根据日期加入(应用)您更改table,每种更改类型一次。
declare @daily_info table ([date] date, id int, [name] varchar(32), place varchar(32));
insert into @daily_info ([date], id, [name], place)
values
('2022-04-11', 1, 'Jhon', null),
('2022-04-12', 1, 'Jhon', null),
('2022-04-13', 1, 'Jhon', null),
('2022-04-14', 1, 'Jhon', null),
('2022-04-15', 1, 'Jhon', null),
('2022-04-16', 1, 'Jhon', null);
declare @change_values table ([date] date, id int, change_col varchar(32), new_value varchar(32));
insert into @change_values ([date], id, change_col, new_value)
values
('2022-04-14', 1, 'place', 'Chicago'),
('2022-04-16', 1, 'place', 'India'),
('2022-04-16', 1, 'name', 'John,Dan');
select DI.[date], DI.id
, coalesce(CV2.new_value, DI.[name]) [name]
, coalesce(CV1.new_value, DI.place) place
from @daily_info DI
outer apply (
select top 1 change_col, new_value
from @change_values CV
where CV.[date] <= DI.[date] and change_col = 'place'
order by [date] desc
) CV1
outer apply (
select top 1 change_col, new_value
from @change_values CV
where CV.[date] <= DI.[date] and change_col = 'name'
order by [date] desc
) CV2;
Returns:
date
id
name
place
2022-04-11
1
Jhon
NULL
2022-04-12
1
Jhon
NULL
2022-04-13
1
Jhon
NULL
2022-04-14
1
Jhon
Chicago
2022-04-15
1
Jhon
Chicago
2022-04-16
1
John,Dan
India
我有这两个table
daily_info
date | id | name | place |
---|---|---|---|
2022-04-11 | 1 | Jhon | null |
2022-04-12 | 1 | Jhon | null |
2022-04-13 | 1 | Jhon | null |
2022-04-14 | 1 | Jhon | null |
2022-04-15 | 1 | Jhon | null |
2022-04-16 | 1 | Jhon | null |
change_values
date | id | change_col | new_value |
---|---|---|---|
2022-04-14 | 1 | place | Chicago |
2022-04-16 | 1 | place | India |
2022-04-16 | 1 | name | John watts |
从这两个 table 中,我想创建一个新的 table,看起来像这样
history_table
date | id | name | place |
---|---|---|---|
2022-04-11 | 1 | Jhon | null |
2022-04-12 | 1 | Jhon | null |
2022-04-13 | 1 | Jhon | null |
2022-04-14 | 1 | Jhon | Chicago |
2022-04-15 | 1 | Jhon | Chicago |
2022-04-16 | 1 | Jhon Watts | India |
到目前为止我只能加入这两个 tables 并尝试使用 case-when 子句,我知道我需要使用滞后但我不是确定如何
我的代码
Select id,
date,
(Case When change_col = 'place' Then new_value End) As place,
(Case When change_col = 'name' Then new_value End) As name
From daily_info
Left Join change_values On d.id = c.id And d.date = c.date
根据日期加入(应用)您更改table,每种更改类型一次。
declare @daily_info table ([date] date, id int, [name] varchar(32), place varchar(32));
insert into @daily_info ([date], id, [name], place)
values
('2022-04-11', 1, 'Jhon', null),
('2022-04-12', 1, 'Jhon', null),
('2022-04-13', 1, 'Jhon', null),
('2022-04-14', 1, 'Jhon', null),
('2022-04-15', 1, 'Jhon', null),
('2022-04-16', 1, 'Jhon', null);
declare @change_values table ([date] date, id int, change_col varchar(32), new_value varchar(32));
insert into @change_values ([date], id, change_col, new_value)
values
('2022-04-14', 1, 'place', 'Chicago'),
('2022-04-16', 1, 'place', 'India'),
('2022-04-16', 1, 'name', 'John,Dan');
select DI.[date], DI.id
, coalesce(CV2.new_value, DI.[name]) [name]
, coalesce(CV1.new_value, DI.place) place
from @daily_info DI
outer apply (
select top 1 change_col, new_value
from @change_values CV
where CV.[date] <= DI.[date] and change_col = 'place'
order by [date] desc
) CV1
outer apply (
select top 1 change_col, new_value
from @change_values CV
where CV.[date] <= DI.[date] and change_col = 'name'
order by [date] desc
) CV2;
Returns:
date | id | name | place |
---|---|---|---|
2022-04-11 | 1 | Jhon | NULL |
2022-04-12 | 1 | Jhon | NULL |
2022-04-13 | 1 | Jhon | NULL |
2022-04-14 | 1 | Jhon | Chicago |
2022-04-15 | 1 | Jhon | Chicago |
2022-04-16 | 1 | John,Dan | India |