Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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-组值列A,仅列B中显示的值不同_Sql_Tsql - Fatal编程技术网

SQL-组值列A,仅列B中显示的值不同

SQL-组值列A,仅列B中显示的值不同,sql,tsql,Sql,Tsql,我正试图写一个查询,以便在使用相同的电子邮件地址或电话号码但邮政编码不同时保持警惕 我有一张如下表: Ref@ | PCode | Email ---------------------------------- LYJX01 | B99 1AA | this@that.com LYJX02 | B98 1AA | this@that.com LYJX03 | B92 1DD | this@that.com AHSF01 | B91 2BB | my@email.com 我想把所有的唱

我正试图写一个查询,以便在使用相同的电子邮件地址或电话号码但邮政编码不同时保持警惕

我有一张如下表:

Ref@   | PCode   | Email
----------------------------------
LYJX01 | B99 1AA | this@that.com
LYJX02 | B98 1AA | this@that.com
LYJX03 | B92 1DD | this@that.com
AHSF01 | B91 2BB | my@email.com
我想把所有的唱片都拿出来this@that.com因为他们的电子邮件地址有多个不同的邮政编码

在理想情况下,邮箱地址的邮政编码应该是唯一的,但如果邮政编码有任何不同,我希望拉这些政策

到目前为止,我写了以下内容:

SELECT pr.B@, pr.Ref@, pr.Pcode, pr.Email
FROM dbo.ic_Daprospect pr
WHERE pr.Email IN (
SELECT Email
FROM dbo.ic_Daprospect pr
GROUP BY pr.Email
HAVING COUNT(*) > 1
)
不过,所有这一切都是在提取所有记录,这些记录包含多行相同邮政编码的行,并且电子邮件地址存在不止一次

我如何只显示他们更改邮政编码的实例


干杯。

您希望首先查找记录了多个邮政编码的所有电子邮件,然后返回此列表中记录的电子邮件所在主表中的所有记录:

select *
from dbo.ic_Daprospect pr
where exists(
            select null
            from dbo.ic_Daprospect pr2
            where pr.Email = pr2.Email    -- This matches any records with the same email as the one in your source table.
            group by pr2.Email            -- Group them to get one row per email.
            having count(distinct pr2.PCode) > 1    -- Then filter for where there is more than one PCode for that email.
            )

在查询性能中使用exists帮助,因为一旦找到匹配项,查询将停止处理子选择。

您希望首先查找记录了多个邮政编码的所有电子邮件,然后返回此列表中记录电子邮件的主表中的所有记录:

select *
from dbo.ic_Daprospect pr
where exists(
            select null
            from dbo.ic_Daprospect pr2
            where pr.Email = pr2.Email    -- This matches any records with the same email as the one in your source table.
            group by pr2.Email            -- Group them to get one row per email.
            having count(distinct pr2.PCode) > 1    -- Then filter for where there is more than one PCode for that email.
            )

在查询性能方面使用exists帮助,因为一旦找到匹配项,查询将停止处理子选择。

请按照要求尝试下面的操作,它还应排除电子邮件id和代码组合相同的记录

  select *
    from dbo.ic_Daprospect pr
    where exists(
                select 1
                from dbo.ic_Daprospect pr2
                where pr.Email=pr2.Email and pr.Email||pr.PCode <> pr2.Email||pr2.PCode
                )

希望它有帮助

尝试下面的要求,它还应该排除电子邮件id和代码组合相同的记录

  select *
    from dbo.ic_Daprospect pr
    where exists(
                select 1
                from dbo.ic_Daprospect pr2
                where pr.Email=pr2.Email and pr.Email||pr.PCode <> pr2.Email||pr2.PCode
                )

希望它能有所帮助

仅仅发布一段没有解释的代码对OP或未来观众了解他们的问题或您的解决方案毫无帮助。仅仅发布一段没有解释的代码对OP或未来观众了解他们的问题或您的解决方案毫无帮助。