【发布时间】:2017-08-25 05:32:03
【问题描述】:
我正在尝试创建一个基于三个条件显示潜在重复记录的报告:SSN 的最后 4 个、姓氏和出生日期。我在这个问题上发布了一个问题here 并收到了一个答案,我应该使用交叉应用来取消透视数据。查询运行速度很快,结果看起来比我原来的查询要好。
新查询如下,我添加了一个过滤器来显示我看到的两个数据示例:
DECLARE
@StartDate DATE = '1/1/2017',
@EndDate DATE = '3/1/2017';
WITH
CTE
AS
(
SELECT
DENSE_RANK() OVER (ORDER BY c.socialSecurityNumber) AS [SSNRanking] ,
c.id AS [CustomerID] ,
c.socialSecurityNumber AS [SSN],
c.firstName AS [FirstName] ,
c.lastName AS [LastName] ,
c.birthDate [BirthDate] ,
c.emailAddress AS [EmailAddress],
c.createDate AS [CreateDate] ,
MAX(co.orderDate) AS [LastOrderDate] ,
ca.street1 AS [Addr1] ,
ca.city AS [City] ,
ca.stateAndTerritoriesID AS [State],
ca.zipCode5 AS [Zip] ,
c2.id AS [DupCustomerID] ,
c2.socialSecurityNumber AS [DupSSN] ,
c2.firstName AS [DupFirstName] ,
c2.lastName AS [DupLastName] ,
c2.birthDate AS [DupBirthDate] ,
c2.emailAddress AS [DupEmailAddress] ,
c2.createDate AS [DupCreateDate] ,
MAX(co.orderDate) AS [DupLastOrderDate] ,
ca.street1 AS [DupAddr1] ,
ca.city AS [DupCity],
ca.stateAndTerritoriesID AS [DupState] ,
ca.zipCode5 AS [DupZip]
FROM
dbo.Customers AS [c]
INNER JOIN dbo.Customers AS [c2] ON ( SUBSTRING(c.socialSecurityNumber,6,4) = SUBSTRING(c2.socialSecurityNumber,6,4) AND c.birthDate = c2.birthDate AND c.lastName = c2.lastName AND c.id <> c2.id )
INNER JOIN dbo.CustomerAddresses AS [ca] ON c.id = ca.customerID
--INNER JOIN dbo.CustomerAddresses AS [ca2] ON ca2.customerID = c2.id
LEFT OUTER JOIN dbo.Common_Orders AS [co] ON co.customerID = c.id
WHERE
c.customerStatusTypeID <> 'M'
AND c2.customerStatusTypeID <> 'M'
AND ca.addressType = 'M'
--AND ca2.addressType = 'M'
AND c.mergedTo IS NULL
AND c2.mergedTo IS NULL
AND CAST(co.orderDate AS DATE) >= @StartDate
AND CAST(co.orderDate AS DATE) <= @EndDate
AND ( c.id = 1545229 OR c.id = 2020489 )
GROUP BY
c.id ,
c.socialSecurityNumber ,
c.firstName ,
c.lastName ,
c.birthDate ,
c.emailAddress ,
c.createDate ,
ca.street1 ,
ca.city ,
ca.stateAndTerritoriesID ,
ca.zipCode5 ,
c2.id ,
c2.socialSecurityNumber ,
c2.firstName ,
c2.lastName ,
c2.birthDate ,
c2.emailAddress ,
c2.createDate ,
ca.street1 ,
ca.city ,
ca.stateAndTerritoriesID ,
ca.zipCode5
)
SELECT CA.CustomerID,
CA.SSNRanking ,
CA.SSN ,
CA.FirstName,
CA.LastName,
CA.BirthDate,
CA.EmailAddress,
CA.CreateDate ,
CA.LastOrderDate ,
CA.Addr1,
CA.City,
CA.[State],
CA.Zip
FROM
CTE
CROSS APPLY
(
VALUES
(CTE.SSNRanking, CTE.CustomerID, CTE.SSN, CTE.FirstName, CTE.LastName, CTE.Birthdate, CTE.EmailAddress, CTE.CreateDate, CTE.LastOrderDate, CTE.Addr1, CTE.City, CTE.[State], CTE.Zip),
(CTE.SSNRanking, CTE.DupCustomerID, CTE.DupSSN, CTE.DupFirstName, CTE.DuplastName, CTE.DupBirthDate, CTE.DupEmailAddress, CTE.DupCreateDate, CTE.DupLastOrderDate, CTE.DupAddr1, CTE.DupCity, CTE.DupState, CTE.DupZip)
) AS CA (SSNRanking, CustomerID, SSN, FirstName, LastName, BirthDate, EmailAddress, CreateDate, LastOrderDate, Addr1, City, [State], Zip)
ORDER BY CAST(CA.SSN AS INT) ASC, CA.CustomerID;
结果集看起来像(Original Image):
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + ------------ + --------------------- + ----------------------- + --------------------- + ------------ + ----- + ----- +
| CustomerId | SSNRanking | SSN | FirstName | LastName | BirthDate | EmailAddress | CreateDate | LastOrderDate | Addr1 | City | State | Zip |
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + ------------ + --------------------- + ----------------------- + --------------------- + ------------ + ----- + ----- +
| 1545229 | 1 | 000000000 | Aquia Boat | SALES | 1900-01-01 | null | 2013-05-28 00:00:00.0 | 2017-01-23 11:08:30.723 | 236 Willow Landing Rd | Stafford | VA | 22554 |
| 1545229 | 1 | 000000000 | Aquia Boat | SALES | 1900-01-01 | null | 2013-05-28 00:00:00.0 | 2017-01-06 12:31:15.370 | 11963 Jefferson Ave | Newport News | VA | 23606 |
| 2020489 | 1 | 000000000 | DIXIE | SALES | 1900-01-01 | null | 2017-01-06 12:27:56.5 | 2017-01-06 12:31:15.370 | 11963 Jefferson Ave | Newport News | VA | 23606 |
| 2020489 | 1 | 000000000 | DIXIE | SALES | 1900-01-01 | null | 2017-01-06 12:27:56.5 | 2017-01-23 11:08:30.723 | 236 Willow Landing Rd | Stafford | VA | 22554 |
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + ------------ + --------------------- + ----------------------- + --------------------- + ------------ + ----- + ----- +
我看到 SSN、姓氏和出生日期的最后四个都匹配。但后来我注意到重复的客户 ID Aqua Boats 有一个带有 Dixie 销售地址的条目,而 Dixie Sales 有一个 Aqua Boats 地址的条目——我不应该有这个条目,我查看了特定帐户的客户/客户地址表:
SELECT c.id, c.socialSecurityNumber, c.firstName, c.lastName, c.birthDate, ca.street1
FROM dbo.Customers AS c
INNER JOIN dbo.CustomerAddresses AS [ca] ON ca.customerID = c.id
WHERE c.id IN (1545229,2020489) AND ca.addressType = 'M';
结果在这里(Original Image):
+ ---------- + -------------------- + ---------- + -------- + ---------- + --------------------- +
| id | socialSecurityNumber | firstName | lastName | birthDate | street 1 |
+ ---------- + -------------------- + ---------- + -------- + ---------- + --------------------- +
| 1545229 | 000000000 | Aquia Boat | SALES | 1900-01-01 | 236 Willow Landing Rd |
| 2020489 | 000000000 | DIXIE | SALES | 1900-01-01 | 11963 Jefferson Ave |
+ ---------- + -------------------- + ---------- + -------- + ---------- + --------------------- +
当我在 CTE 中运行查询时,我添加了两个额外的过滤器:
AND c.id <> ca2.customerID
AND c2.id <> ca.customerID
数据集看起来像我想要的 (Original Image):
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + --------------------- + ------------ + ----- + ----- + ------------- + --------- + ------------- + ----------- + ------------- + --------------------- + ------------ + -------- + ------ +
| SSNRanking | CustomerID | SSN | FirstName | LastName | BirthDate | Addr1 | City | State | Zip | DupCustomerID | DupSSN | DupFirstName | DupLastName | DupBirthDate | DupAddr1 | DupCity | DupState | DupZip |
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + --------------------- + ------------ + ----- + ----- + ------------- + --------- + ------------- + ----------- + ------------- + --------------------- + ------------ + -------- + ------ +
| 1 | 1545229 | 000000000 | Aquia Boat | SALES | 1900-01-01 | 236 Willow Landing Rd | Stafford | VA | 22554 | 2020589 | 000000000 | DIXIE | SALES | 1900-01-01 | 236 Willow Landing Rd | Stafford | VA | 22554 |
| 1 | 2020489 | 000000000 | DIXIE | SALES | 1900-01-01 | 11963 Jefferson Ave | Newport News | VA | 23606 | 1545229 | 000000000 | AQUIA BOAT | SALES | 1900-01-01 | 11963 Jefferson Ave | Newport News | VA | 23606 |
+ ---------- + ---------- + --------- + ---------- + -------- + ---------- + --------------------- + ------------ + ----- + ----- + ------------- + --------- + ------------- + ----------- + ------------- + --------------------- + ------------ + -------- + ------ +
我可以阻止 CROSS APPLY 为邮寄地址分配给其他客户的客户创建额外记录吗?
谢谢,
【问题讨论】:
标签: sql sql-server tsql sql-server-2014 cross-apply