在 SQL 语句中没有返回准确的结果

Having in SQL statement isn't returning accurate results

我有一个查询 returns 来自临时 table 的客户数据。 temp table 仅存储客户编号的名字和姓氏。它有大约 320 条符合项目资格的人的记录。

create table #customers
(
customer_no int,
fname varchar(20),
lname varchar(55)
)

我需要从数据库中做的是说给我整个数据库中具有名字和姓氏组合的每个人(以查找重复项)。我们还需要提取地址,但我们不想在搜索中使用客户编号,因为我们以后需要它,但现在我们希望严格地比较名字和姓氏。

select  distinct a.customer_no,
        a.fname,
        a.mname,
        a.lname,
        b.street1,
        b.street2,
        b.city,
        b.state,
        b.postal_code
from    T_CUSTOMER a
join    T_ADDRESS b on a.customer_no = b.customer_no
where   (
        lname in (select lname from #customers) and 
        fname in (select fname from #customers)
        )
order by a.lname, a.fname

所以查询返回了一个很长的记录列表 - 但是因为有很多重复的组合(它多次返回客户编号)。

例如:

customer_no fname   mname   lname   street1             city
86224371    John    NULL    Cornid  11 Maplewood Dr.    New Haven
86224371    John    NULL    Cornid  11 Maplewood Drive  Hartford
86313525    John    NULL    Cornid  11 Maplewood Dr     Hartford
86390546    John    Seth    Cornid  11 Maplewood dr.    New Haven

所以数据是正确的。 唯一的问题是我想取消第一行 - 我想说只给我唯一的客户号码。我希望我的输出是:

    customer_no fname   mname   lname   street1             city
    86224371    John    NULL    Cornid  11 Maplewood Dr.    New Haven
    86313525    John    NULL    Cornid  11 Maplewood Dr     Hartford
    86390546    John    Seth    Cornid  11 Maplewood dr.    New Haven

我们必须提供基本信息来清理数据 -- 我们只希望每个客户号码拉一次。

select  distinct a.customer_no,
        a.fname,
        a.mname,
        a.lname,
        b.street1,
        b.street2,
        b.city,
        b.state,
        b.postal_code
from    T_CUSTOMER a
join    T_ADDRESS b on a.customer_no = b.customer_no
where   (
        lname in (select lname from #customers) and 
        fname in (select fname from #customers)
        )
group by    a.customer_no,
            a.fname,
            a.mname,
            a.lname,
            b.street1,
            b.street2,
            b.city,
            b.state,
            b.postal_code
having count(a.customer_no) > 1
order by a.lname, a.fname

我在上面写了包含计数 customer_no > 1 但它不起作用,因为它 returns 是一个空数据集。

请指教。 谢谢你。

您需要使用连接

select
        a.customer_no,
        a.fname,
        a.mname,
        a.lname,
        b.street1,
        b.street2,
        b.city,
        b.state,
        b.postal_code
from T_CUSTOMER a
join T_ADDRESS b on a.customer_no = b.customer_no
join #customers c on a.lname = c.lname and a.fname = c.fname
order by a.lname, a.fname

附带说明一下,如果您必须使用 distinct,那么您可能做错了。


有时您需要这样做:

join #customers c on upper(a.lname) = upper(c.lname) and upper(a.fname) = upper(c.fname)

有时您需要这样做:

join #customers c on trim(a.lname) = trim(c.lname) and trim(a.fname) = trim(c.fname)

这可能是一个不同的问题,但在下面的评论中

We have a record with multiple addresses and its being returned and counted multiple times. But i only want that to happen if the names are different - and customer numbers are different

只有 "count" 我会这样做:

select * from
(
  select a.customer_no,
        a.fname,
        a.mname,
        a.lname,
        b.street1,
        b.street2,
        b.city,
        b.state,
        b.postal_code
        row_number(partition by a.fname, a.lname, a.customer_no
                   order by street1, stree2) as rn
  from    T_CUSTOMER a
  join    T_ADDRESS b on a.customer_no = b.customer_no
) x
where rn = 1

不,我会建议一个比 street1 更好的字段作为排序依据,例如更新日期或创建日期。