SQL有条件地从行中扣除一些数字

时间:2019-05-17 09:38:47

标签: tsql sql-server-2017

我有一张桌子(显示临时表)

CREATE TABLE #TempTable
(
    TempID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    CustID INT NOT NULL,
    RODate DATE NULL,
    Operation INT NULL
);

此表具有以下示例数据:

INSERT INTO #TempTable (CustID, RODate, Operation)
VALUES  (10, DATEADD(MONT, -2, GETDATE()), 2),
        (10, DATEADD(MONT, -1, GETDATE()), 3),
        (10, GETDATE(), 5)

因此表格具有以下数据

TempID  CustID                  RODate          Operation
-----------------------------------------------------------
1         10                    2019-03-17          2
2         10                    2019-04-17          3
3         10                    2019-05-17          5

要求是我将在参数@noOfOperation中得到一个整数变量,假设它的值为10

我也不会得到几个月的参数,比如说3个月

我必须查询表以仅返回最近3个月的数据(不包括当月(日期升序)

然后我必须从表中扣除@noOfOperation并进行更新。

扣除额将基于操作列中的可用性。

例如:在这种情况下,我们将从2019-03-17扣除

10 - 2 = 8 (operation column for this row  becomes 0)

接下来,我们将从2019-04-17中扣除

8 - 3 = 5 (operation column for this row  becomes 0)

类似于2019-05-17

5-5 = 0 (operation column for this row  becomes 0)

我必须检查@noOfOperation是否小于或大于各个月的手术次数,然后相应地执行上述操作

2 个答案:

答案 0 :(得分:1)

检查一下,其想法是使用累加运算,然后减去所需的值,如下所示:

declare @TempTable TABLE 
(
    TempID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    CustID INT NOT NULL,
    RODate DATE NULL,
    Operation INT NULL
);


INSERT INTO @TempTable (CustID, RODate, Operation)
VALUES  (10, DATEADD(MONTH, -2, GETDATE()), 5),
        (10, DATEADD(MONTH, -1, GETDATE()), 6),
        (10, GETDATE(), 7)

        select * from @TempTable


        Declare @noOfOperation int =8
        Declare @noOfMonths  int =3

        Declare @StartDate date,@DateEnd date,@avNoOfOperation int

        --get the range you are working for
        select 
         @StartDate=cast(cast(year(dateadd(Month,-@noOfMonths+1,getdate())) as varchar(4))+'-'+cast(Month(dateadd(Month,-@noOfMonths+1,getdate())) as varchar(2))+'-01' as date) 
         ,@DateEnd=dateadd(day,-1,cast(cast(year(getdate()) as varchar(4))+'-'+cast(Month(getdate()) as varchar(2))+'-01' as date)) ;

        --get the total of avaliable operation, for validating before subtracting
        select @avNoOfOperation=sum(t.Operation) from @TempTable t where cast(t.RODate as date) between @StartDate and @DateEnd

        --review the variables if needed
        --select @StartDate [@StartDate],@DateEnd [@DateEnd],@avNoOfOperation [@avNoOfOperation]

        if(@avNoOfOperation>=@noOfOperation and @noOfOperation>0)
        begin
            --only here we can start subtracting
                ;with DataIncluded as (
                    select *,@noOfOperation [noOfOperation],sum(Operation) over (order by RODate) [AcOp] from @TempTable t where cast(t.RODate as date) between @StartDate and @DateEnd
                ),SubtractDataSet as (
                    select  *,AcOp-@noOfOperation [leftOp],
                    case when (AcOp-@noOfOperation)<=0 then 0 else 
                        case when (AcOp-@noOfOperation)<Operation then AcOp-@noOfOperation else Operation end  end [UpOp]
                    from DataIncluded 
                )
                Update @TempTable 
                    set A.Operation=B.[UpOp] 
                    From @TempTable A
                        inner join SubtractDataSet B on A.TempID=B.TempID

        end

select * from @TempTable

注意:我未使用当月,因此我的输出与您建议的输出不同。如果输入如下:

TempID  CustID  RODate      Operation
1       10      2019-03-17  5
2       10      2019-04-17  6
3       10      2019-05-17  7

输出为:-

TempID  CustID  RODate      Operation
1       10      2019-03-17  0
2       10      2019-04-17  3
3       10      2019-05-17  7

答案 1 :(得分:0)

--Change the value of @OperationsToBeDeducted, to see different results

declare @OperationsToBeDeducted int
declare @OperationsRemaining int
declare @RODate date

set @OperationsToBeDeducted = 4

declare @TempID int
set @TempID = 1

DROP TABLE IF EXISTS #TempOperation
create table #TempOperation
            (
                TempID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
                CustID INT NOT NULL,
                RODate DATE NULL,
                Operation INT NULL
            );

insert into #TempOperation (CustID,RODate,Operation)
values
(10,DATEADD(month, -3, getdate()),2),
(10,DATEADD(month, -2, getdate()), 2),
(10,DATEADD(month, -1, getdate()),3)


DROP TABLE IF EXISTS #TempOperation2
create table #TempOperation2
            (
                TempID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
                CustID INT NOT NULL,
                RODate DATE NULL,
                Operation INT NULL
            );
insert into #TempOperation2 select CustID,RODate, Operation from #TempOperation

select * from #TempOperation2 order by RODate asc

declare @maxID int;
select @maxID = max(TempID) from #TempOperation2

while (@TempID <= @maxID)
begin

    set @OperationsRemaining = 0

    select @OperationsRemaining = Operation, @RODate = RODate from #TempOperation2 where TempID = @TempID

    if(@OperationsToBeDeducted is not null and @OperationsRemaining is not null and 
    @OperationsRemaining > 0 and @OperationsRemaining > @OperationsToBeDeducted)
    begin
        update #TempOperation set Operation = @OperationsRemaining - @OperationsToBeDeducted where TempID = @TempID
        set @OperationsToBeDeducted = 0
    end
    else if(@OperationsToBeDeducted is not null and @OperationsRemaining is not null and 
    @OperationsRemaining > 0 and @OperationsRemaining <= @OperationsToBeDeducted)
    begin
        set @OperationsToBeDeducted = @OperationsToBeDeducted - @OperationsRemaining
        update #TempOperation set Operation = @OperationsRemaining - @OperationsRemaining where TempID = @TempID

    end
    SET @TempID  = @TempID  + 1
end
select * from #TempOperation order by RODate asc

DROP TABLE #TempOperation
DROP TABLE #TempOperation2