I have two tables:
Table “one”:
ServiceID, ApplicationCode, Success
1, 1, 2
1, 3, 2
2, NULL, 3
Table “two”:
ServiceID, ApplicationCode, Failure
1, 1, 1
1, 2, 3
2, NULL, 4
3, NULL, 1
I want to receive that result table:
Columns:
ServiceID, ApplicationCode, Success, Failure
1, 1, 2, 1
1, 3, 2, NULL
2, NULL, 3, 4
1, 2, NULL, 3
3, NULL, NULL, 1
I’m using SQL Server 2008.
What query should I use ?
EDITED: I’m looking to join the two tables by ServiceID and ApplicationCode.
EDITED 2:
Code I’ve tried:
INSERT INTO #MidResult(ServiceID,ApplicationCode,SuccessCount,FailureCount)
SELECT case rtrim(ltrim(s.ServiceID)) WHEN NULL THEN f.ServiceID ELSE s.ServiceID END,s.ApplicationCode,s.SuccessCount,f.FailureCount
FROM #SuccessResult s
FULL JOIN #FailureResult f on f.ApplicationCode = s.ApplicationCode and s.ServiceID = f.ServiceID
You might be able to use the following:
See SQL Fiddle with Demo.
The result is: