SQL连接同一个表并创建单行结果以完成空值
我在SQL中只有一个源表。我正试图将它自身连接起来,以便填充空值并将优先级值保留在一行中。有数百万条记录SQL连接同一个表并创建单行结果以完成空值,sql,sql-server,sql-server-2008,ssms,self-join,Sql,Sql Server,Sql Server 2008,Ssms,Self Join,我在SQL中只有一个源表。我正试图将它自身连接起来,以便填充空值并将优先级值保留在一行中。有数百万条记录 ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc 8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL 9 1asd23 b555cc Source2 NULL
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
由此:
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
为此(预期结果):
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
那么,我如何将表连接到它自身以获得单行结果呢
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
我尝试过使用下面的查询,并将其作为一个视图,但仅检索一行就需要永远执行
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
--CREATE VIEW dbo.vw_JOIN AS
WITH MyTABLE AS (
SELECT * FROM TABLE1
WHERE Owner = 'Source2')
SELECT
T1.ID1,
T1.Source1ID,
T2.Source2ID,
T1.Owner,
T1.Source1Date,
T1.Source1Desc,
T2.Source2Date
T2.Source2Desc
FROM TABLE1 T1
LEFT JOIN MyTABLE T2
ON T1.Source1ID = T2.Source1ID
OR T1.Source2ID = T2.Source2ID
WHERE T1.Owner = 'Source1'
这就是你想要的吗
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
select min(id1) as id1, Source1ID, min(Owner) as owner,
min(Source2ID) as Source2ID, min(Source1Desc) as Source1Desc,
min(Source2Date) as Source2Date, min(Source2Desc) as Source2Desc
from t
group by source1ID;
declare @table table (id1 int, source1id varchar(50),source2Id varchar(50),[owner] varchar(50),Source1Date date,Source1Desc varchar(50),Source2Date date, source2Desc varchar(50)
)
insert into @table
values
(8, '1asd23', 'a567nm' ,'Source1' ,'1/1/1900' , 'Active', NULL , NULL),
(9 , '1asd23', 'b555cc' ,'Source2', NULL , NULL , '12/1/2000', 'Ongoing')
select a.id1,a.source1id,b.source2Id,a.owner,a.Source1Date,a.Source1Desc,b.Source2Date,b.source2Desc from @table a
inner join @table b on a.source1id = b.source1id and a.source2Id != b.source2Id where a.owner ='source1'
这是你想要的吗
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 a567nm Source1 1/1/1900 Active NULL NULL
9 1asd23 b555cc Source2 NULL NULL 12/1/2000 Ongoing
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 1asd23 b555cc Source1 1/1/1900 Active 12/1/2000 Ongoing
select min(id1) as id1, Source1ID, min(Owner) as owner,
min(Source2ID) as Source2ID, min(Source1Desc) as Source1Desc,
min(Source2Date) as Source2Date, min(Source2Desc) as Source2Desc
from t
group by source1ID;
declare @table table (id1 int, source1id varchar(50),source2Id varchar(50),[owner] varchar(50),Source1Date date,Source1Desc varchar(50),Source2Date date, source2Desc varchar(50)
)
insert into @table
values
(8, '1asd23', 'a567nm' ,'Source1' ,'1/1/1900' , 'Active', NULL , NULL),
(9 , '1asd23', 'b555cc' ,'Source2', NULL , NULL , '12/1/2000', 'Ongoing')
select a.id1,a.source1id,b.source2Id,a.owner,a.Source1Date,a.Source1Desc,b.Source2Date,b.source2Desc from @table a
inner join @table b on a.source1id = b.source1id and a.source2Id != b.source2Id where a.owner ='source1'
@SQLKnows,尝试相同的查询,但使用MAX()而不是MIN()。您不清楚自己想要什么。使用足够的单词和句子使其清晰。另外,请注意。此代码仅适用于此示例。我不知道你剩下的数据是怎样的,所以它可能会崩溃。否则,请使用Gordons的示例,并在值错误的地方使用MAX