-1 null join sql-server
我有两个要加入的表
|id |profit|
| 1 | 1234 |
| 2 | 1345 |
| 4 | 1454 |
| 5 | 1254 |
Run Code Online (Sandbox Code Playgroud)
和另一张桌子
| id | x loss | y loss |
| 2 | 34312 | 4354 |
| 3 | 35614 | 4365 |
| 4 | 36615 | 4321 |
Run Code Online (Sandbox Code Playgroud)
并结合两者,这将是我想要的结果
| id | profit | x loss | y loss |
| 1 | 1234 | Null | Null |
| 2 | 1345 | 34312 | 4354 |
| 3 | Null | 35614 | 4365 |
| 4 | 1454 | 36615 | 4321 |
| 5 | 1254 | Null | Null |
Run Code Online (Sandbox Code Playgroud)
我使用的代码是
Select x.id, y.id, y.profit, x.xloss, x.yloss
from
(select id, SUM(xloss) as xloss, SUM(yloss) AS yloss
from #SYSLOSSINC
group by id) x
full outer join
(Select id,sum(profit) as profit
from #MTDPROFIT
group by ProductCode) y
on x.id = y.id
Run Code Online (Sandbox Code Playgroud)
由于错误,我无法仅选择 id: "ambiguous column name 'id'",这是创建两个带有 id 的列
| id | id | Profit | xloss | yloss|
| Null | 1 | 1234 | Null | Null |
| 2 | 2 | 1345 | 34312 | 4354 |
| 3 | Null | Null | 35614 | 4365 |
| 4 | 4 | 1454 | 36615 | 4321 |
| Null | 5 | 1254 | Null | Null |
Run Code Online (Sandbox Code Playgroud)
如何将 id 合并到一列中,以便不显示 null?
| 归档时间: |
|
| 查看次数: |
378 次 |
| 最近记录: |