-2

我有两张桌子:

表“一”:

ServiceID, ApplicationCode, Success
1, 1, 2
1, 3, 2
2, NULL, 3

表“二”:

ServiceID, ApplicationCode, Failure
1, 1, 1
1, 2, 3
2, NULL, 4
3, NULL, 1

我想收到那个结果表

列:

ServiceID, ApplicationCode, Success, Failure
1, 1, 2, 1
1, 3, 2, NULL
2, NULL, 3, 4
1, 2, NULL, 3
3, NULL, NULL, 1

我正在使用 SQL Server 2008。

我应该使用什么查询?

已编辑:我希望通过 ServiceID 和 ApplicationCode 加入这两个表。

编辑2: 我试过的代码:

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
4

2 回答 2

3

您也许可以使用以下内容:

select 
  coalesce(t1.serviceid, t2.serviceid) serviceid,
  coalesce(t1.ApplicationCode, t2.ApplicationCode) ApplicationCode,
  t1.Success,
  t2.failure
from table1 t1
full outer join table2 t2
  on t1.ServiceID = t2.ServiceID
  and isnull(t1.ApplicationCode, '') = isnull(t2.ApplicationCode, '')
order by serviceid, ApplicationCode

请参阅SQL Fiddle with Demo

结果是:

| SERVICEID | APPLICATIONCODE | SUCCESS | FAILURE |
---------------------------------------------------
|         1 |               1 |       2 |       1 |
|         1 |               2 |  (null) |       3 |
|         1 |               3 |       2 |  (null) |
|         2 |          (null) |       3 |       4 |
|         3 |          (null) |  (null) |       1 |
于 2013-01-14T11:05:29.693 回答
-1
Select one.ServiceID, one.ApplicationCode, one.Success, two.Failure
From TableOne one , TableTwo two
where one.ServiceID = two.ServiceID
and one.ApplicationCode = two.ApplicationCode

只需注意 ServiceID 和 ApplicationCode 都应该匹配。

于 2013-01-14T11:01:05.157 回答