SQL 对 A 列进行内部联接并比较 B 列中的值
SQL inner join on column A and compare values in column B
我确信或者希望有一种简单的方法可以做到这一点。下面是我最近的尝试。查询应该 return 只有一行,但我得到 31.
查询:
`select distinct a.[EC CERS], a.[EC Tank ID], [CIW Tank ID]
from #tempectank a
inner join #tempciwtank b on a.[EC CERS] = b.[CIW CERS]
where a.[EC Tank ID] <> b.[CIW Tank ID]`
Table数据:
Table一个
EC CERS
EC Tank ID
100338
1
100338
2
100338
3
Table B
CIW CERS
CIW Tank ID
100338
001
100338
2
100338
3
Table 架构
create table #tempectank(
s int IDENTITY (1, 1),
[EC CERS] varchar(9),
[EC Tank ID] varchar(255)
)
我需要查询 return 一行,最好显示 EC CERS、EC Tank ID 和 CIW Tank ID。
如果您想要 a.[EC CERS] = b.[CIW CERS] 和 a.[EC Tank ID] <> b.[CIW Tank ID] 的行,请尝试以下操作:
Select a.[EC CERS], a.[EC Tank ID], b.[CIW Tank ID]
from #tempectank a
inner join #tempciwtank b
on a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] <> b.[CIW Tank ID]
如果是leading ziros的问题,你可以这样解决:
如果您的 ID 中包含非数字,这将不起作用
Select a.[EC CERS], a.[EC Tank ID], b.[CIW Tank ID]
from #tempectank a
inner join #tempciwtank b
on a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] <> b.[CIW Tank ID]
and cast(a.[EC Tank ID] as int ) = cast(b.[CIW Tank ID] as int)
实际上,另一种解决方案是显示找不到匹配项的内容:
select * from #tempectank a
where not exists (select 1 from #tempciwtank b
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempciwtank b where a.[EC CERS] = b.[CIW CERS])
如果你想并排显示不匹配的案例,你必须从两边找到不匹配的实例:
select * from (
select * from #tempectank a
where not exists (select 1 from #tempciwtank b
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempciwtank b where a.[EC CERS] = b.[CIW CERS])
) a
join (
select * from #tempciwtank b
where not exists (select 1 from #tempectank a
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempectank a where a.[EC CERS] = b.[CIW CERS])
)b
on a.[EC CERS] = b.[CIW CERS]
我确信或者希望有一种简单的方法可以做到这一点。下面是我最近的尝试。查询应该 return 只有一行,但我得到 31.
查询:
`select distinct a.[EC CERS], a.[EC Tank ID], [CIW Tank ID]
from #tempectank a
inner join #tempciwtank b on a.[EC CERS] = b.[CIW CERS]
where a.[EC Tank ID] <> b.[CIW Tank ID]`
Table数据:
Table一个
EC CERS | EC Tank ID |
---|---|
100338 | 1 |
100338 | 2 |
100338 | 3 |
Table B
CIW CERS | CIW Tank ID |
---|---|
100338 | 001 |
100338 | 2 |
100338 | 3 |
Table 架构
create table #tempectank(
s int IDENTITY (1, 1),
[EC CERS] varchar(9),
[EC Tank ID] varchar(255)
)
我需要查询 return 一行,最好显示 EC CERS、EC Tank ID 和 CIW Tank ID。
如果您想要 a.[EC CERS] = b.[CIW CERS] 和 a.[EC Tank ID] <> b.[CIW Tank ID] 的行,请尝试以下操作:
Select a.[EC CERS], a.[EC Tank ID], b.[CIW Tank ID]
from #tempectank a
inner join #tempciwtank b
on a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] <> b.[CIW Tank ID]
如果是leading ziros的问题,你可以这样解决:
如果您的 ID 中包含非数字,这将不起作用
Select a.[EC CERS], a.[EC Tank ID], b.[CIW Tank ID]
from #tempectank a
inner join #tempciwtank b
on a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] <> b.[CIW Tank ID]
and cast(a.[EC Tank ID] as int ) = cast(b.[CIW Tank ID] as int)
实际上,另一种解决方案是显示找不到匹配项的内容:
select * from #tempectank a
where not exists (select 1 from #tempciwtank b
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempciwtank b where a.[EC CERS] = b.[CIW CERS])
如果你想并排显示不匹配的案例,你必须从两边找到不匹配的实例:
select * from (
select * from #tempectank a
where not exists (select 1 from #tempciwtank b
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempciwtank b where a.[EC CERS] = b.[CIW CERS])
) a
join (
select * from #tempciwtank b
where not exists (select 1 from #tempectank a
where a.[EC CERS] = b.[CIW CERS]
and a.[EC Tank ID] = b.[CIW Tank ID]
)
and exists (select 1 from #tempectank a where a.[EC CERS] = b.[CIW CERS])
)b
on a.[EC CERS] = b.[CIW CERS]