查询临时表中的重复行会导致行不重复

时间:2018-10-11 17:44:40

标签: sql-server tsql

为了进行复制,我构建了以下临时表并用数据填充了它:

CREATE TABLE #TempActualsImport 
(
        AID                 INT                 NULL,
        PostedDate          NVARCHAR(255)       NULL,
        AccountingPeriod    NVARCHAR(255)       NULL,
        JournalDate         NVARCHAR(255)       NULL,
        JournalID           NVARCHAR(255)       NULL,
        JournalDescription  NVARCHAR(1000)      NULL,
        JournalLine         INT                 NULL, 
        PaymentAmt          NVARCHAR(255)       NULL,
        RelativeAmount      NUMERIC(32,2)       NULL,
        IRSDuplicateRows    BIT                 DEFAULT 0 NULL, 
        Recstamp            Timestamp
)

以下查询根据以下条件更新临时表,以查找称为IRS重复项的重复项。

UPDATE #TempActualsImport 
SET IRSDuplicateRows = 1
WHERE AID IN (SELECT t1.AID 
              FROM #TempActualsImport t1, #TempActualsImport t2 
              WHERE t1.PostedDate = t2.PostedDate
                AND t1.AccountingPeriod = t2.AccountingPeriod
                AND t1.JournalDate = t2.JournalDate
                AND t1.JournalID = t2.JournalID
                AND t1.JournalDescription = t2.JournalDescription
                AND t1.JournalLine = t2.JournalLine
                AND t1.PaymentAmt = t2.PaymentAmt
                AND t1.RelativeAmount = t2.RelativeAmount
                AND CONVERT(DATE, t1.PostedDate) = '2018-10-03'   
                AND t1.RelativeAmount = 0
                AND t1.AID = t2.AID)

针对临时表运行以下查询:

SELECT
    AID, PostedDate, AccountingPeriod, 
    JournalDate, JournalID, JournalDescription, JournalLine, PaymentAmt, 
    RelativeAmount, IRSDuplicateRows
FROM
    #TempActualsImport 
WHERE
    IRSDuplicateRows = 1

以下两行被标记为重复项,但很明显JournalLine(中的第7列)与这两条记录不匹配。

589 20181003    4   20181002    PR00154645  10/02/2018 Payroll  8399         
0.00    1

808 20181003    4   20181002    PR00154645  10/02/2018 Payroll  15888        
0.00    1

我希望这两条记录不会显示为IRS重复项,因为JournalLine列不匹配。我究竟做错了什么?

我已经在SQL Server 2008 R2和SQL Server 2016上对此进行了测试,结果相同。

以下是一些示例数据,用于填充#TempActualsImport:

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (589, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 8399, N'', CAST(0.00 AS Numeric(32, 
2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (711, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12486, N'', CAST(1846.96 AS 
Numeric(32, 2)))
GO

 INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
 [JournalDate], [JournalID], [JournalDescription], [JournalLine], 
 [PaymentAmt], [RelativeAmount]) VALUES (712, N'20181003', N'4', 
 N'20181002', N'PR00154645', N'10/02/2018 Payroll', 12487, N'', CAST(169.33 
 AS Numeric(32, 2)))
 GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (713, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12488, N'', CAST(205.46 AS Numeric(32, 
2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (714, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12489, N'', CAST(121.95 AS Numeric(32, 
2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (715, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12490, N'', CAST(19.48 AS Numeric(32, 
2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (716, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12663, N'', CAST(1538.85 AS 
Numeric(32, 2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (717, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 12664, N'', CAST(13.57 AS Numeric(32, 
2)))
GO

INSERT [#TempActualsImport] ([AID], [POSTEDDate], [AccountingPeriod], 
[JournalDate], [JournalID], [JournalDescription], [JournalLine], 
[PaymentAmt], [RelativeAmount]) VALUES (808, N'20181003', N'4', N'20181002', 
N'PR00154645', N'10/02/2018 Payroll', 15888, N'', CAST(0.00 AS Numeric(32, 
2)))
GO

2 个答案:

答案 0 :(得分:0)

完成测试,以下内容可以解决问题,并通过使用公用表表达式查找我要检查的重复项。 (注意:必须在WITH之前添加;才能在存储过程中进行编译)

;WITH CTE AS
(SELECT *, ROW_NUMBER() OVER (PARTITION BY PostedDate, AccountingPeriod, JournalDate, 
JournalID,  JournalLine,JournalDescription,
    PaymentAmt, RelativeAmount
ORDER BY (SELECT 0)) AS DuplicateRowNumber
    FROM #TempActualsImport
    WHERE CONVERT(DATE,PostedDate)  = '2018-10-03' 
    AND PaymentAmt = 0
)
Update CTE SET IRSDuplicateRows = 1 
where DuplicateRowNumber > 1

答案 1 :(得分:0)

好吧,既然我已经使用提供的示例代码亲自运行了查询,那么我可以解释为什么看到了所看到的行为。

正如我在评论中所发布的,我希望您用来将每一行标记为重复的更新。原因是当您将表联接到自身时,由于联接的每一行都会匹配,因此每一行在联接的另一侧都会有匹配的行。

但是,在您的情况下,RelativeAmount列中唯一匹配并标记为重复的两行是带有“ 0.00”的行。这样做的原因是我之前没有注意到的UPDATE中的这一行:

AND t1.RelativeAmount = 0

注释该行,表中的所有行(发布日期为10/3)将被标记为重复行。