【问题标题】:SQL query for duplicate customers重复客户的 SQL 查询
【发布时间】:2017-07-07 13:19:47
【问题描述】:

我想要客户数量(计数)并从表中复制 phone_no。我必须删除电话号码中的破折号,然后检查长度是否等于 10。请有人帮忙处理这个 SQL 查询吗?

我的查询如下:

select 
    COUNT(Cust_ID) cnt, Phone_no 
from 
    tbl_Customers 
where 
    Cust_Type = 3
group by 
    Phone_no
having 
    COUNT(Cust_ID) > 1 
order by 
    cnt desc

【问题讨论】:

  • 你的预期输出是什么
  • 计算并复制phone_no。我真正的问题是,如何在 where 条件下删除破折号并获取 phone_no 的长度?

标签: sql-server-2008 sql-server-2012


【解决方案1】:

在您的 WHERE 子句中添加 LEN(REPLACE(Phone_no,'-','')) = 10 应该是您要查找的内容。

select COUNT(Cust_ID) cnt,Phone_no 
from tbl_Customers 
where Cust_Type = 3
      and
      LEN(REPLACE(Phone_no,'-','')) = 10
group by Phone_no
having COUNT(Cust_ID) > 1 
order by cnt desc

这是这些功能的 Microsoft 文档。

REPLACE

LEN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    相关资源
    最近更新 更多