我有一个方案,我将第一个文件file_id 8018加载到阶段表中 然后将第二个文件与file_id 8019一起加载到阶段表中。
在第一个文件中可以得到相同的记录。
在我发布的结果集中,我们可以看到(2,3,4)记录中具有file_id 8018的id在第二个文件中,具有file_id 8019
id estimatename market spotlength bcastweek goaldollars file_id
2 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-08 15499.75 8018
3 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-15 15499.75 8018
4 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-22 15499.75 8018
5 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-24 32453.44 8018
58 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-08 15499.75 8019
59 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-15 15499.75 8019
60 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-22 15499.75 8019
62 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-05 15499.75 8019
63 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-12 15499.75 8019
64 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-19 15499.75 8019
我希望结果设置为
id estimatename market spotlength bcastweek goaldollars file_id
5 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-24 32453.44 8018
58 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-08 15499.75 8019
59 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-15 15499.75 8019
60 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-07-22 15499.75 8019
62 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-05 15499.75 8019
63 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-12 15499.75 8019
64 3Q19 CALIFORNIA LOS ANGELES, CA :60 2019-08-19 15499.75 8019
当第二个文件中存在相同记录时,我想从第一个文件中删除记录。
答案 0 :(得分:1)
一种方法是聚合:
select max(id) as id, estimatename, market, spotlength, bcastweek, goaldollars,
max(file_id) as file_id
from t
group by estimatename, market, spotlength, bcastweek, goaldollars;
如果您有两个单独的文件,则也可以使用not exists
将它们合并:
select t2.*
from staging2 t2
union all
select t1.*
from staging1 t1
where not exists (select 1
from staging2 t2
where t2.estimatename = t1.estimatename and
t2.market = t1.market and
. . .
);
答案 1 :(得分:1)
要显示结果,可以使用CTE:
表格:
Create Table #tbl1
(
id Int,
estimatename VarChar(10),
market VarChar(30),
spotlength VarChar(3),
bcastweek Date,
goaldollars Decimal(12,2),
[file_id] Int
)
Insert Into #tbl1 Values
(2,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-08',15499.75,8018),
(3,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-15',15499.75,8018),
(4,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-22',15499.75,8018),
(5,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-24',32453.44,8018),
(58,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-08',15499.75,8019),
(59,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-15',15499.75,8019),
(60,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-22',15499.75,8019),
(62,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-08-05',15499.75,8019),
(63,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-08-12',15499.75,8019),
(64,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-08-19',15499.75,8019)
Create Table #tbl2
(
id Int,
estimatename VarChar(10),
market VarChar(30),
spotlength VarChar(3),
bcastweek Date,
goaldollars Decimal(12,2),
[file_id] Int
)
Insert Into #tbl2 Values
(2,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-08',15499.75,8018),
(3,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-15',15499.75,8018),
(4,'3Q19','CALIFORNIA LOS ANGELES, CA',':60','2019-07-22',15499.75,8018)
CTE:
With cte As
(
Select
t1.*,
t2.id As t2_id
From
#tbl1 t1 Left Join
#tbl2 t2 On t1.id = t2.id
Where
t2.id Is Null
)
Select * From cte
要执行表1中的删除操作:
Delete From #tbl1
From
#tbl1 t1 Left Join
#tbl2 t2 On t1.id = t2.id
Where
t2.id Is Not Null
Select * from #tbl1
答案 2 :(得分:0)
--Try this sample and see if it helps you
CREATE TABLE #tmpstaging
(id int,
estimatename VARCHAR(100),
market VARCHAR(100) , spotlength VARCHAR(100) ,bcastweek VARCHAR(100) , goaldollars decimal(18,2)
,file_id INT
)
INSERT INTO #tmpstaging(id, estimatename , market , spotlength , bcastweek , goaldollars, FILE_ID) values
(2 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-08' ,15499.75 , 8018)
,(3 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-15' ,15499.75 ,8018)
,(4 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-22' ,15499.75 , 8018)
,(5 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-24' ,32453.44 , 8018)
,(58 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-08' ,15499.75 ,8019)
,(59 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-15' , 15499.75 , 8019)
,(60 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-07-22' ,15499.75 , 8019)
,(62 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-08-05' , 15499.75 , 8019)
,(63 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-08-12' , 15499.75 , 8019)
,(64 , '3Q19','CALIFORNIA LOS ANGELES', 'CA :60' ,'2019-08-19' ,15499.75 ,8019)
DELETE FROM #tmpstaging WHERE id NOT IN
(SELECT Max(id) FROM #tmpstaging
GROUP BY estimatename, market, spotlength ,bcastweek, goaldollars
)
SELECT * FROM #tmpstaging
DROP TABLE #tmpstaging