2

您好,抱歉,这可能是一个很长的问题,我有一个奇怪的问题突然出现了。我正在尝试创建一个类似会计的应用程序,其中几个列按它们所属的组聚合。但是,其中一些列可能包含我不想包含在总数中的重复数据。

我的桌子看起来像:

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             |              |              |  |  |  |
+-----+-----------------------+----------+-------------+-------------------------------+------------------+----------+--------------+--------+------------------+-------------+-----------------------+----------------+----------------------+---------------+---------------------+----------------+-------------------------+-------------------+--------------+--------------+--+--+--+
4

2 回答 2

1

我们可以一次性完成吗?

create procedure [dbo].[dedup_and_calc] (
    @startMonth int
  , @endMonth int
  , @tvp dbo.tvp readonly 
) as
begin;
  set nocount on;

  declare @date date = (
    select eomonth(max(checkdate))
    from @tvp
    where month(checkdate) >= @startmonth 
      and month(checkdate) <= @endmonth
      );
  /* using row_number() to zero out using rn > 1 in query below */
  ;with cte as (
    select *
     , rn = row_number() over (
         partition by CheckDate, PropNum, PropSeqNum, PropName, ProductionMonth, Product
         order by Header /* There is no Id from @tvp ?*/
         ) 
         /* artifically increase rownumber for Product >= 400 */
         + case when Product >= 400 then 1 else 0 end 
    from @tvp
  )
  insert into RawDataTable(
      CheckDate
    , PropNum
    , PropSeqNum
    , PropName
    , ProductionMonth
    , Product
    , LeaseVolume
    , Price
    , LeaseGrossValue
    , LeaseTaxes
    , LeaseOtherDeductions
    , LeaseNetValue
    , DisbursementDecimal
    , InterestType
    , InterestGrossValue
    , InterestTaxes
    , IntrestOtherDeductions
    , InterestNetValue
  )
  select
       CheckDate             
     , PropNum
     , PropSeqNum            
     , PropName
     , ProductionMonth       
     , Product
     , LeaseVolume           = case when rn =1 then LeaseVolume else 0 end
     , Price                 = case when rn =1 then Price else 0 end
     , LeaseGrossValue       = case when rn =1 then LeaseGrossValue else 0 end
     , LeaseTaxes            = case when rn =1 then LeaseTaxes else 0 end
     , LeaseOtherDeductions  = case when rn =1 then LeaseOtherDeductions else 0 end
     , LeaseNetValue         = case when rn =1 then LeaseNetValue else 0 end
     , DisbursementDecimal   
     , InterestType          
     , InterestGrossValue    = InterestGrossValue
     , InterestTaxes         = InterestTaxes
     , IntrestOtherDeductions= IntrestOtherDeductions
     , InterestNetValue      = InterestNetValue
    from cte 
    where month(CheckDate) >= @startMonth 
      and month(CheckDate) <= @endMonth
  union all 
  select
     CheckDate             = @date
   , PropNum
   , PropSeqNum            = null
   , PropName
   , ProductionMonth       = null
   , Product
   , LeaseVolume           = sum(case when rn =1 then LeaseVolume else null end)
   , Price                 = sum(case when rn =1 then Price else null end)
   , LeaseGrossValue       = sum(case when rn =1 then LeaseGrossValue else null end)
   , LeaseTaxes            = sum(case when rn =1 then LeaseTaxes else null end)
   , LeaseOtherDeductions  = sum(case when rn =1 then LeaseOtherDeductions else null end)
   , LeaseNetValue         = sum(case when rn =1 then LeaseNetValue else null end)
   , DisbursementDecimal   = null
   , InterestType          = null
   , InterestGrossValue    = sum(InterestGrossValue)
   , InterestTaxes         = sum(InterestTaxes)
   , IntrestOtherDeductions= sum(IntrestOtherDeductions)
   , InterestNetValue      = sum(InterestNetValue)
  from cte 
  where month(CheckDate) >= @startMonth 
    and month(CheckDate) <= @endMonth
  group by PropNum, PropName, Product 
end;
go


测试:

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)
);
go
create type dbo.tvp as table (
    Header                  int
  , OwnerNumber             int
  , CheckNum                varchar(32)
  , CheckDate               date
  , PropNum                 int
  , PropSeqNum              int
  , PropName                varchar(64)
  , ProductionMonth         int
  , Product                 int
  , LeaseVolume             decimal (18, 2)
  , Price                   decimal (18, 2)
  , LeaseGrossValue         decimal (18, 2)
  , LeaseTaxes              decimal (18, 2)
  , LeaseOtherDeductions    decimal (18, 2)
  , LeaseNetValue           decimal (18, 2)
  , DisbursementDecimal     decimal (18, 2)
  , InterestType            varchar(10)
  , InterestGrossValue      decimal (18, 2)
  , InterestTaxes           decimal (18, 2)
  , IntrestOtherDeductions  decimal (18, 2)
  , InterestNetValue        decimal (18, 2)
  --, RecordCount             decimal (18, 2)
  --, CheckAmount             decimal (18, 2)
)

go
create procedure [dbo].[dedup_and_calc] (
    @startMonth int
  , @endMonth int
  , @tvp dbo.tvp readonly 
) as
begin;
  set nocount on;
  declare @date date = (
    select NextMonthStart = dateadd(day,-1,dateadd(month, datediff(month, 0,max(checkdate))+1, 0))
    from @tvp
    where month(checkdate) >= @startmonth 
      and month(checkdate) <= @endmonth
      );
  /* using row_number() to zero out using rn > 1 in query below */
  ;with cte as (
    select *
     , rn = row_number() over (
         partition by CheckDate, PropNum, PropSeqNum, PropName, ProductionMonth, Product
         order by Header
         ) 
         /* artifically increase rownumber for Product >= 400 */
         + case when Product >= 400 then 1 else 0 end 
    from @tvp
  )
  insert into RawDataTable(
      CheckDate
    , PropNum
    , PropSeqNum
    , PropName
    , ProductionMonth
    , Product
    , LeaseVolume
    , Price
    , LeaseGrossValue
    , LeaseTaxes
    , LeaseOtherDeductions
    , LeaseNetValue
    , DisbursementDecimal
    , InterestType
    , InterestGrossValue
    , InterestTaxes
    , IntrestOtherDeductions
    , InterestNetValue
  )
  select
       CheckDate             
     , PropNum
     , PropSeqNum            
     , PropName
     , ProductionMonth       
     , Product
     , LeaseVolume           = case when rn =1 then LeaseVolume else 0 end
     , Price                 = case when rn =1 then Price else 0 end
     , LeaseGrossValue       = case when rn =1 then LeaseGrossValue else 0 end
     , LeaseTaxes            = case when rn =1 then LeaseTaxes else 0 end
     , LeaseOtherDeductions  = case when rn =1 then LeaseOtherDeductions else 0 end
     , LeaseNetValue         = case when rn =1 then LeaseNetValue else 0 end
     , DisbursementDecimal   
     , InterestType          
     , InterestGrossValue    = InterestGrossValue
     , InterestTaxes         = InterestTaxes
     , IntrestOtherDeductions= IntrestOtherDeductions
     , InterestNetValue      = InterestNetValue
    from cte 
    where month(CheckDate) >= @startMonth 
      and month(CheckDate) <= @endMonth
  union all 
  select
     CheckDate             = @date
   , PropNum
   , PropSeqNum            = null
   , PropName
   , ProductionMonth       = null
   , Product
   , LeaseVolume           = sum(case when rn =1 then LeaseVolume else null end)
   , Price                 = sum(case when rn =1 then Price else null end)
   , LeaseGrossValue       = sum(case when rn =1 then LeaseGrossValue else null end)
   , LeaseTaxes            = sum(case when rn =1 then LeaseTaxes else null end)
   , LeaseOtherDeductions  = sum(case when rn =1 then LeaseOtherDeductions else null end)
   , LeaseNetValue         = sum(case when rn =1 then LeaseNetValue else null end)
   , DisbursementDecimal   = null
   , InterestType          = null
   , InterestGrossValue    = sum(InterestGrossValue)
   , InterestTaxes         = sum(InterestTaxes)
   , IntrestOtherDeductions= sum(IntrestOtherDeductions)
   , InterestNetValue      = sum(InterestNetValue)
  from cte 
  where month(CheckDate) >= @startMonth 
    and month(CheckDate) <= @endMonth
  group by PropNum, PropName, Product 

end;
go
declare @tvp dbo.tvp
insert into @tvp values
  ('1','13280701','E008613928','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','13280701','E008613928','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')
exec [dbo].[dedup_and_calc] 1,3, @tvp 
select * from RawDataTable as rdt

drop table rawdatatable
drop procedure dedup_and_calc
drop type dbo.tvp

返回:

+----+------------+---------+------------+-------------------------------+-----------------+---------+-------------+-------+-----------------+------------+----------------------+---------------+---------------------+--------------+--------------------+---------------+------------------------+------------------+
| Id | CheckDate  | PropNum | PropSeqNum |           PropName            | ProductionMonth | Product | LeaseVolume | Price | LeaseGrossValue | LeaseTaxes | LeaseOtherDeductions | LeaseNetValue | DisbursementDecimal | InterestType | InterestGrossValue | InterestTaxes | IntrestOtherDeductions | InterestNetValue |
+----+------------+---------+------------+-------------------------------+-----------------+---------+-------------+-------+-----------------+------------+----------------------+---------------+---------------------+--------------+--------------------+---------------+------------------------+------------------+
|  1 | 2015-01-25 |  100004 | 25         | BEAVER LODGE DEVON UT TR-0025 | 122014          |     100 | 774.96      | 51.93 | 40243.64        | -4628.03   | 0.00                 | 35615.61      | 0                   | RI 01        | 108.38             | -12.46        | 0.00                   | 95.92            |
|  2 | 2015-01-25 |  100004 | 25         | BEAVER LODGE DEVON UT TR-0025 | 122014          |     100 | 0.00        | 0.00  | 0.00            | 0.00       | 0.00                 | 0.00          | 0                   | RI 02        | 203.91             | -23.45        | 0.00                   | 180.46           |
|  3 | 2015-01-31 |  100004 | NULL       | BEAVER LODGE DEVON UT TR-0025 | NULL            |     100 | 774.96      | 51.93 | 40243.64        | -4628.03   | 0.00                 | 35615.61      | NULL                | NULL         | 312.29             | -35.91        | 0.00                   | 276.38           |
+----+------------+---------+------------+-------------------------------+-----------------+---------+-------------+-------+-----------------+------------+----------------------+---------------+---------------------+--------------+--------------------+---------------+------------------------+------------------+
于 2017-02-20T20:51:30.117 回答
0

好吧,这只是我的一个愚蠢的错误。我有一个触发两次的拖放事件,因为我以编程方式在我的 Form_Load 中声明了它,并且它也在我的表单设计器中声明。它花了比我愿意承认的时间更长的时间来解决这个问题(但你可能可以从发布时间到我提交这个时间)。

对于遇到此问题的其他任何人,如果您的代码中有类似的内容:

AllowDrop = true;
DragEnter += new DragEventHandler(Form1_DragEnter);
DragDrop += new DragEventHandler(Form1_DragDrop);

检查表单的设计器是否有类似的东西。尝试从您的代码中注释它,看看它是否修复它。

也只想对任何试图帮助我的人说声谢谢!

于 2017-02-21T07:57:31.523 回答