SQL Join Same Table and create single row result to complete NULL values


SQL Join Same Table and create single row result to complete NULL values
I have only one source table in SQL. I am trying to join it to itself so that i can fill up NULL values and retain the priority values into single row. There are million of records.
FROM THIS:
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 123 567 Source1 1/1/1900 Active NULL NULL
9 123 555 Source2 NULL NULL 12/1/2000 Ongoing
TO THIS (expected result):
ID1 Source1ID Source2ID Owner Source1Date Source1Desc Source2Date Source2Desc
8 123 555 Source1 1/1/1900 Active 12/1/2000 Ongoing
So how can i join the table to itself to attain that single row result?
I have tried using the query below and made it as a VIEW but it takes forever to execute just to retrieve 1 row.
--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'
Screenshot of Table values
1 Answer
1
Is this what you want?
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;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.