重复客户的SQL查询

时间:2017-07-07 13:19:47

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

我想从表中获得多个客户(计数)和重复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

1 个答案:

答案 0 :(得分:0)

LEN(REPLACE(Phone_no,'-','')) = 10子句中添加WHERE应该是您想要的。

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