您好,抱歉,这可能是一个很长的问题,我有一个奇怪的问题突然出现了。我正在尝试创建一个类似会计的应用程序,其中几个列按它们所属的组聚合。但是,其中一些列可能包含我不想包含在总数中的重复数据。
我的桌子看起来像:
CREATE TABLE [dbo].[RawDataTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[CheckDate] DATE NULL,
[PropNum] INT NOT NULL,
[PropSeqNum] INT NULL,
[PropName] NVARCHAR (100) NOT NULL,
[ProductionMonth] INT NULL,
[Product] INT NOT NULL,
[LeaseVolume] DECIMAL (18, 2) NOT NULL,
[Price] DECIMAL (18, 2) NOT NULL,
[LeaseGrossValue] DECIMAL (18, 2) NOT NULL,
[LeaseTaxes] DECIMAL (18, 2) NULL,
[LeaseOtherDeductions] DECIMAL (18, 2) NOT NULL,
[LeaseNetValue] DECIMAL (18, 2) NOT NULL,
[DisbursementDecimal] DECIMAL (18) NULL,
[InterestType] NVARCHAR (10) NULL,
[InterestGrossValue] DECIMAL (18, 2) NOT NULL,
[InterestTaxes] DECIMAL (18, 2) NOT NULL,
[IntrestOtherDeductions] DECIMAL (18, 2) NOT NULL,
[InterestNetValue] DECIMAL (18, 2) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
首先,我尝试使用此存储过程删除任何重复数据:
CREATE PROCEDURE [dbo].[EraseDuplicates]
AS
UPDATE T1
SET T1.LeaseVolume = 0,
T1.Price = 0,
T1.LeaseGrossValue = 0,
T1.LeaseTaxes = 0,
T1.LeaseOtherDeductions = 0,
T1.LeaseNetValue = 0
FROM
(SELECT *
FROM RawDataTable
WHERE Product >= 400 OR
Id NOT IN
(SELECT MIN(Id)
FROM RawDataTable
GROUP BY CheckDate, PropNum, PropSeqNum, PropName, ProductionMonth, Product)) AS T1
当我从 EraseDuplicates 存储过程返回时,我的问题出现了,它正确地将数据清零,但是它也在复制行并将它们添加回表中,结果如下:
主要编辑
我已将其范围缩小到此存储过程。一旦我从这种方法返回
public static void EraseDuplicateData(string connString)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings[connString].ConnectionString))
{
db.Query("EraseDuplicates", commandType: CommandType.StoredProcedure);
}
}
数据库突然复制了行。这些值被正确归零,但我只是不明白更新命令如何创建行。
原始数据
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| CheckDate | PropNum | PropSeqNum | PropName | ProductionMonth | Product | LeaseVolume | Price | LeaseGrossValue | LeaseTaxes | LeaseOtherDeductions | LeaseNetValue | DisbursementDecimal | InterestType | InterestGrossValue | InterestTaxes | IntrestOtherDeductions | InterestNetValue | RecordCount | CheckAmount | | | |
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| 1/25/2015 | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 774.96 | 51.93 | 40243.64 | -4628.03 | 0 | 35615.61 | 0.0026932 | RI 01 | 108.38 | -12.46 | 0 | 95.92 | | | | | |
| 1/25/2015 | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 774.96 | 51.93 | 40243.64 | -4628.03 | 0 | 35615.61 | 0.0050669 | RI 02 | 203.91 | -23.45 | 0 | 180.46 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 204 | -0.27 | 4.037 | -1.09 | -0.11 | 0.13 | -1.07 | 0.0026932 | RI 01 | -0.01 | 0 | 0 | -0.01 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 116.76 | 3.992 | 466.11 | -60.63 | -1511.54 | -1106.06 | 0.0026932 | RI 01 | 1.26 | -0.16 | -4.07 | -2.97 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 116.76 | 3.992 | 466.11 | -60.63 | -1511.54 | -1106.06 | 0.0050669 | RI 02 | 2.36 | -0.31 | -7.66 | -5.61 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 400 | -1.59 | 1.1006 | -1.75 | 0 | 0 | -1.75 | 0.0050669 | RI 02 | -0.01 | 0 | 0 | -0.01 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 3380.17 | 0.6214 | 2100.4 | 0 | 0 | 2100.4 | 0.0026932 | RI 01 | 5.66 | 0 | 0 | 5.66 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 3380.17 | 0.6214 | 2100.4 | 0 | 0 | 2100.4 | 0.0050669 | RI 02 | 10.64 | 0 | 0 | 10.64 | | | | | |
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
预期结果
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| CheckDate | PropNum | PropSeqNum | PropName | ProductionMonth | Product | LeaseVolume | Price | LeaseGrossValue | LeaseTaxes | LeaseOtherDeductions | LeaseNetValue | DisbursementDecimal | InterestType | InterestGrossValue | InterestTaxes | IntrestOtherDeductions | InterestNetValue | RecordCount | CheckAmount | | | |
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| 1/25/2015 | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 774.96 | 51.93 | 40243.64 | -4628.03 | 0 | 35615.61 | 0.0026932 | RI 01 | 108.38 | -12.46 | 0 | 95.92 | | | | | |
| 1/25/2015 | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0.0050669 | RI 02 | 203.91 | -23.45 | 0 | 180.46 | | | | | |
| 1/31/2015 | 100004 | 25 | BEAVER LODGE DEVON UT TR-0026 | | 100 | 774.96 | 51.93 | 40243.64 | -4628.03 | 0 | 35615.61 | | | 312.29 | -35.91 | 0 | 276.38 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 204 | -0.27 | 4.037 | -1.09 | -0.11 | 0.13 | -1.07 | 0.0026932 | RI 01 | -0.01 | 0 | 0 | -0.01 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 116.76 | 3.992 | 466.11 | -60.63 | -1511.54 | -1106.06 | 0.0026932 | RI 01 | 1.26 | -0.16 | -4.07 | -2.97 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 0 | 0 | 0 | 0 | 0 | 0 | 0.0050669 | RI 02 | 2.36 | -0.31 | -7.66 | -5.61 | | | | | |
| 1/31/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | | 204 | 116.49 | 8.029 | 465.02 | -60.74 | -1511.41 | -1107.13 | | | 3.61 | -0.47 | -11.73 | -8.59 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 400 | -1.59 | 1.1006 | -1.75 | 0 | 0 | -1.75 | 0.0050669 | RI 02 | -0.01 | 0 | 0 | -0.01 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 3380.17 | 0.6214 | 2100.4 | 0 | 0 | 2100.4 | 0.0026932 | RI 01 | 5.66 | 0 | 0 | 5.66 | | | | | |
| 1/25/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 0 | 0 | 0 | 0 | 0 | 0 | 0.0050669 | RI 02 | 10.64 | 0 | 0 | 10.64 | | | | | |
| 1/31/2015 | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | | 400 | 3378.58 | 1.722 | 2098.65 | 0 | 0 | 2098.65 | | | 16.29 | 0 | 0 | 16.29 | | | | | |
+------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
实际结果(在 EraseDuplicates 程序之后)
+-----+-----------------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| Id | CheckDate | PropNum | PropSeqNum | PropName | ProductionMonth | Product | LeaseVolume | Price | LeaseGrossValue | LeaseTaxes | LeaseOtherDeductions | LeaseNetValue | DisbursementDecimal | InterestType | InterestGrossValue | InterestTaxes | IntrestOtherDeductions | InterestNetValue | RecordCount | CheckAmount | | | |
+-----+-----------------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
| 464 | 1/1/0001 12:00:00 AM | NULL | NULL | | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | NULL | NULL | NULL | NULL | | | | | |
| 926 | 1/1/0001 12:00:00 AM | NULL | NULL | | NULL | NULL | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | NULL | | NULL | NULL | NULL | NULL | | | | | |
| 465 | 1/25/2015 12:00:00 AM | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 203.91 | -23.45 | 0.00 | 180.46 | | | | | |
| 1 | 1/25/2015 12:00:00 AM | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 774.96 | 51.93 | 40243.64 | -4628.03 | 0.00 | 35615.61 | 0.0026932000 | RI 01 | 108.38 | -12.46 | 0.00 | 95.92 | | | | | |
| 2 | 1/25/2015 12:00:00 AM | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 203.91 | -23.45 | 0.00 | 180.46 | | | | | |
| 29 | 1/25/2015 12:00:00 AM | 100004 | 25 | BEAVER LODGE DEVON UT TR-0025 | 122014 | 100 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0026932000 | RI 01 | 108.38 | -12.46 | 0.00 | 95.92 | | | | | |
| 44 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 204 | -0.27 | 4.03 | -1.09 | -0.11 | 0.13 | -1.07 | 0.0026932000 | RI 01 | -0.01 | 0.00 | 0.00 | -0.01 | | | | | |
| 46 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 116.76 | 3.99 | 466.11 | -60.63 | -1511.54 | -1106.06 | 0.0026932000 | RI 01 | 1.26 | -0.16 | -4.07 | -2.97 | | | | | |
| 47 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 2.36 | -0.31 | -7.66 | -5.61 | | | | | |
| 506 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 204 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0026932000 | RI 01 | -0.01 | 0.00 | 0.00 | -0.01 | | | | | |
| 508 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0026932000 | RI 01 | 1.26 | -0.16 | -4.07 | -2.97 | | | | | |
| 509 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 204 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 2.36 | -0.31 | -7.66 | -5.61 | | | | | |
| 510 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0026932000 | RI 01 | 5.66 | 0.00 | 0.00 | 5.66 | | | | | |
| 511 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 10.64 | 0.00 | 0.00 | 10.64 | | | | | |
| 507 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | -0.01 | 0.00 | 0.00 | -0.01 | | | | | |
| 48 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0026932000 | RI 01 | 5.66 | 0.00 | 0.00 | 5.66 | | | | | |
| 49 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 112014 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | 10.64 | 0.00 | 0.00 | 10.64 | | | | | |
| 45 | 1/25/2015 12:00:00 AM | 100004 | 40111 | BEAVER LODGE DEVON UT TR-0025 | 42013 | 400 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.0050669000 | RI 02 | -0.01 | 0.00 | 0.00 | -0.01 | | | | | |
+-----+-----------------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+