用于在1中合并2行的SQL Server查询
有一个SQL Server表(请参见下面的屏幕截图),我无法更改: 产品具有标识符和工艺参数。有两个进程,A和B。每个进程将数据存储在自己的行中 我想得到一个没有无用空值的表结果。一行一个产品,没有更多。所需的单元格将高亮显示。有关所需输出,请参见第二个屏幕截图: 自动连接?(编辑) 所以 我已经对此进行了快速测试,我认为它看起来是正确的。自动加入?(编辑)用于在1中合并2行的SQL Server查询,sql,sql-server,tsql,Sql,Sql Server,Tsql,有一个SQL Server表(请参见下面的屏幕截图),我无法更改: 产品具有标识符和工艺参数。有两个进程,A和B。每个进程将数据存储在自己的行中 我想得到一个没有无用空值的表结果。一行一个产品,没有更多。所需的单元格将高亮显示。有关所需输出,请参见第二个屏幕截图: 自动连接?(编辑) 所以 我已经对此进行了快速测试,我认为它看起来是正确的。自动加入?(编辑) select a.id, isnull(b.state, a.state) as state, is
select a.id,
isnull(b.state, a.state) as state,
isnull(b.process, a.process) as process,
isnull(b.length, a.length) as length,
isnull(b.force, a.force) as force,
isnull(b.angle, a.angle) as angle
from table as a
left join table as b
on a.id = b.id
and b.process = 'B'
where a.process = 'A'
DECLARE @T AS TABLE (id int, state varchar(10), process varchar(10), length int, angle int
primary key (id, process));
insert into @t (id, state, process, length, angle) values
(111, 'OK', 'A', 77, null)
,(111, 'OK', 'B', null, 30)
,(159, 'NOK', 'A', 89, null)
,(147, 'OK', 'A', 78, null)
,(147, 'NOK', 'B', null, 36);
select ta.id, --ta.*, tb.*
isnull(tb.state, ta.state) as state,
isnull(tb.process, ta.process) as process,
isnull(tb.length, ta.length) as length,
isnull(tb.angle, ta.angle) as angle
from @t ta
left join @t tb
on ta.id = tb.id
and tb.process = 'B'
where ta.process = 'A'
order by ta.id
所以
我已经对此进行了快速测试,我认为它看起来是正确的。因此,电视上的人们更喜欢基于文本的表格,而不是图像。:)提供您尝试过的SQL是礼貌的。没有文本数据,人们不会进行测试。因此,人们更喜欢包含图像的基于文本的表。:)提供您尝试过的SQL是有礼貌的。如果没有文本数据,人们将不会进行测试。where将中断左侧连接是的,我假设每个状态都有A和B进程,这可能是一个错误。我会更正…示例数据并不都有A和B。如果它们都有,那么您就不需要左连接。请参阅我的答案。你们只需要把它移出where,where会打断左边的连接是的,我假设每个州都有A和B进程,这可能是个错误。我会更正…示例数据并不都有A和B。如果它们都有,那么您就不需要左连接。请参阅我的答案。你们只需要把它搬走。
select a.id,
isnull(b.state, a.state) as state,
isnull(b.process, a.process) as process,
isnull(b.length, a.length) as length,
isnull(b.force, a.force) as force,
isnull(b.angle, a.angle) as angle
from table as a
left join table as b
on a.id = b.id
and b.process = 'B'
where a.process = 'A'
DECLARE @T AS TABLE (id int, state varchar(10), process varchar(10), length int, angle int
primary key (id, process));
insert into @t (id, state, process, length, angle) values
(111, 'OK', 'A', 77, null)
,(111, 'OK', 'B', null, 30)
,(159, 'NOK', 'A', 89, null)
,(147, 'OK', 'A', 78, null)
,(147, 'NOK', 'B', null, 36);
select ta.id, --ta.*, tb.*
isnull(tb.state, ta.state) as state,
isnull(tb.process, ta.process) as process,
isnull(tb.length, ta.length) as length,
isnull(tb.angle, ta.angle) as angle
from @t ta
left join @t tb
on ta.id = tb.id
and tb.process = 'B'
where ta.process = 'A'
order by ta.id