在没有PK的情况下在CTE上进行INNER JOIN(公用表表达式)

时间:2014-04-18 21:42:29

标签: sql sql-server

我有一个CTE,我在其中找到了匹配5列的重复记录:

    ;WITH DuplicateCount AS
    (
    SELECT 
                   FirstName, 
                   LastName, 
                   DateofBirth,  
                   Email,  
                   c1.Status, 
                   Count(*) AS TotalCount
    FROM Customer c
    INNER JOIN Customer_1 c1 ON c1.customerID = c.customerID
    GROUP BY   FirstName, LastName, DateofBirth, Email, c1.Status
    HAVING COUNT(*) > 1
    )

然后我从该CTE中选择Status和TotalCount并加入Enum表以生成可读数据

;WITH DuplicateCount AS
(
SELECT 
               FirstName, 
               LastName, 
               DateofBirth,  
               Email,  
               c1.Status, 
               Count(*) AS TotalCount
FROM Customer c
INNER JOIN Customer_1 c1 ON c1.customerID = c.customerID
GROUP BY   FirstName, LastName, DateofBirth, Email, c1.Status
HAVING COUNT(*) > 1
)

SELECT e.Display, dc.TotalCount
FROM DuplicateCount dc
INNER JOIN Enum e ON dc.Status = e.Index

在这种情况下,我可以撤回可读数据并使用Excel按状态吐出重复图表报告。

问题

我需要再次加入Customer_1表再收集一列:Stage。以下是我尝试的方法:

;WITH DuplicateCount AS
(
SELECT         customerID,
               FirstName, 
               LastName, 
               DateofBirth,  
               Email,  
               c1.Status, 
               Count(*) AS TotalCount
FROM Customer c
INNER JOIN Customer_1 c1 ON c1.customerID = c.customerID
GROUP BY   customerID, FirstName, LastName, DateofBirth, Email, c1.Status
HAVING COUNT(*) > 1
)

SELECT e.Display, 
       CASE 
            WHEN c1.Stage = 6 THEN 'First'
            WHEN c1.Stage = 7 THEN 'Second'
            WHEN c1.Stage = 8 THEN 'Third'
            WHEN c1.Stage = 11 THEN 'Fourth'
            WHEN c1.Stage = 9 THEN 'Fifth'
            WHEN c1.Stage = 10 THEN 'Sixth'
            WHEN c1.Stage = 12 THEN 'Unknown'
            ELSE ''
       END AS Stage,
       dc.TotalCount
FROM DuplicateCount dc
INNER JOIN Enum e ON dc.Status = e.Index
INNER JOIN Customer_1 c1 ON c1.customerID = dc.customerID

显然,这没有用,因为我的记录都不会有重复的PK。

有没有办法将表格连接到我的CTE 而不用 PK?或者以某种方式将PK添加到我的CTE 而不用分组?

修改:这就是我想要实现的目标


| FirstName |姓氏|舞台|总计数

|约翰|史密斯|首先| 2

|约翰|史密斯|第三| 2

|亚历克斯|史密斯|首先| 2

|简|史密斯|第三| 2

|简|史密斯|首先| 2

|杰克|史密斯|第二| 2

然后,在报告此数据时:

  • John Smith共有4条记录。两个在第一个,两个在第三

  • Alex Smith总共有2条记录。两个在第一

  • Jane Smith共有4条记录。两个在第一个,两个在第三个

  • 杰克史密斯共有2项记录。两个在第二

绘制这些数据时,我应该能够看到:

  • 第一次:共计6次。

  • 第二次:共2次。

  • 第三次:共4次。

理想情况下,我还可以引入CreatedDate并开始收集以下数据:

  • 每个舞台有多少重复。

  • 每人有多少重复。

  • 特定日期范围,事件等的重复次数

1 个答案:

答案 0 :(得分:0)

两组数据的基数不匹配。我的意思是,第一组具有所识别的重复数据的数据是跨多个客户的聚合数据(不识别任何客户)。然后,您无法获取多个单独的客户ID,并将它们归还给聚合行。

我认为您需要做的是重新构建您想要从数据中获取的内容并向后工作。发布您尝试实现的一组示例结果。

更新:

您似乎想要一个带有计数的客户\阶段组列表?:

SELECT     customerID,
           FirstName, 
           LastName, 
           DateofBirth,  
           Email,  
           c1.Status, 
       CASE 
        WHEN c1.Stage = 6 THEN 'First'
        WHEN c1.Stage = 7 THEN 'Second'
        WHEN c1.Stage = 8 THEN 'Third'
        WHEN c1.Stage = 11 THEN 'Fourth'
        WHEN c1.Stage = 9 THEN 'Fifth'
        WHEN c1.Stage = 10 THEN 'Sixth'
        WHEN c1.Stage = 12 THEN 'Unknown'
        ELSE ''
   END AS Stage,
           Count(*) AS TotalCount

FROM Customer c
INNER JOIN Customer_1 c1 ON c1.customerID = c.customerID
GROUP BY   customerID, FirstName, LastName, DateofBirth, Email, c1.Status, c1.Stage
HAVING COUNT(*) > 1