gpt4 book ai didi

c# - SQL 查询正在复制行并在我的聚合函数中使用这些行

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:07 24 4
gpt4 key购买 nike

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

我的 table 看起来像:

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

实际结果(删除重复程序后)

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

最佳答案

我们可以一次完成吗?

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

关于c# - SQL 查询正在复制行并在我的聚合函数中使用这些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com