Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查找一列值匹配而另一列值不匹配的行_Sql_Sql Server - Fatal编程技术网

Sql 查找一列值匹配而另一列值不匹配的行

Sql 查找一列值匹配而另一列值不匹配的行,sql,sql-server,Sql,Sql Server,我有两张桌子A和B 表A CODE TYPE A 1 A 2 A 3 B 1 C 1 C 2 表B CODE TYPE A 1 A 2 A 4 B 2 C 1 C 3 我想返回代码在两个表中都存在但类型不存在的行,并且代码在两个表中都有多个类型,因此我的结果是 CODE TYPE SOURCE A 3 Table A A 4 Table B C 2 Table A C 3 Table B 有什么帮助吗?您可以使用完全联接来查看代码是否匹配,并检查两个表上的

我有两张桌子A和B

表A

CODE TYPE
A 1
A 2
A 3
B 1
C 1
C 2
表B

CODE TYPE
A 1
A 2
A 4
B 2
C 1
C 3
我想返回代码在两个表中都存在但类型不存在的行,并且代码在两个表中都有多个类型,因此我的结果是

 CODE TYPE SOURCE
    A 3 Table A
    A 4 Table B
    C 2 Table A
    C 3 Table B
有什么帮助吗?

您可以使用完全联接来查看代码是否匹配,并检查两个表上的类型是否为null

select coalesce(a.code,b.code) code, coalesce(a.type,b.type) type,
case when b.type is null then 'A' when a.type is null then 'B' end src
from a
full join b on a.code = b.code and a.type = b.type
where a.type is null or b.type is null
要将结果限制为具有多个类型的代码,请使用

select x.code, coalesce(a.type,b.type) type,
case when b.type is null then 'Table A' when a.type is null then 'Table B' end src
from a
full join b on a.code = b.code and a.type = b.type
join (select a.code from a join b on a.code = b.code 
      group by a.code having count(*) > 1) x on x.code = a.code or x.code = b.code
where a.type is null or b.type is null
order by 1

我认为这涵盖了你的两种情况

select code, coalesce(typeA, typeB) as type, src
from
    (
    select
        coalesce(a.code, b.code) as code,
        a.type as typeA,
        b.type as typeB,
        case when b.type is null then 'A' when a.type is null then 'B' end as src,
        count(a.code) over (partition by coalesce(a.code, b.code)) as countA,
        count(b.code) over (partition by coalesce(a.code, b.code)) as countB
    from
        A a full outer join B b
            on b.code = a.code and b.type = a.type
    ) T
where
        countA >= 2 and countB >= 2
    and (typeA is null or typeB is null)
使用联合

with tu as (
    select CODE, TYPE, src='Table A'
    from TableA
    union all
    select CODE, TYPE, src='Table B'
    from TableB
)
select CODE, TYPE, max(src)
from tu t1
where exists (select 1 from tu t2 where t2.CODE=t1.CODE and t2.src=t1.src and t1.TYPE <> t2.TYPE)
group by CODE, TYPE
having count(*)=1
order by CODE, TYPE

结果也应该有B 1和B 2。从A.CODE=B.CODE上的内部联接B中选择*,其中A.TYPE B.TYPE和A.CODE在countCode>1的group by CODE中选择代码,B.CODE在countCode>1的group by by CODE中选择代码我不希望结果中有B 1和B 2源A必填字段…?别名A&B将不可用在外部的WHERE条件下可用SelectThank@shawnt00您提供的解决方案适用于我,如果说代码/类型列在表A中有重复记录,因为计数不明确,这不会崩溃吗?例如A的所有表A记录,其中5,那么A 5 A仍然会显示,但它实际上只是表A中的1种类型?在他的示例数据中,这不会是一个问题,因为要键入的代码是唯一的。@Matt是的,我确信这会把事情搞砸。谢谢@vkp的解决方案。