我正在尝试编写一个查询,查找名称何时更改但客户ID不会更改。为此,我使用了
Group by CustID, Name
然后计算客户ID,其中任何大于1的结果都很重要。这是我到目前为止的查询:
Select
CustID,
NumDiffNames
from
(select
CustID,
count(CustID) NumDiffNames
from
(Select
CustID,
Name
from
(select
CustID,
concat(rtrim(ltrim(FIRSTNAME)),rtrim(ltrim(LASTNAME))) Name
FROM
[Branch]
union
select
coCustID as CustID,
concat(rtrim(ltrim(COFNAME)),rtrim(ltrim(COLNAME))) Name
FROM
[Branch]) a
group by CustID, Name) b
group by CustID) c
where NumDiffNames>1
但是,我得到了许多误报。我通过修剪每个名称周围的空白来删除大量的空格。另一组误报由数据输入和拼写错误组成。例如,在一种情况下,客户名称拼写为Vanessa,对于相同的客户ID,名称拼写为Venessa。或者在一种情况下拼写正确的名称,但在另一个约翰&以Jhon为例。
是否可以让组具有更多差异,例如一个或两个字符差异?
答案 0 :(得分:0)
你能用这样的东西吗?
WITH CTE AS (
SELECT DISTINCT
CustID
,[name]
,ROW_NUMBER() OVER (PARTITION BY CustID, [Name] ORDER BY CustID) AS RowNumber
FROM Branch )
SELECT *
FROM CTE
WHERE RowNumber > 1