1

我知道问题是什么,但任何人都可以建议如何解决此查询。

问题是当我在 dateadd 中计算几个月时,我得到的价值超过 98540 :(

declare @Basepool int =10000000
declare @transactioncount bigint =1
declare @Monthnum int=1
declare @ContractId int=1

select CASE @Basepool 
            WHEN 0 THEN 'N/A'
            WHEN -1 THEN 'N/A'
            ELSE CASE 
                    WHEN SUM(SUM(@TransactionCount)) OVER (Partition by @ContractId) > @Basepool THEN 'IN-OVERAGE'
                    --WHEN SUM(SUM(B.TransactionCount)) OVER (Partition by @ContractId) + (SUM(SUM(B.TransactionCount))  OVER (Partition by @ContractId)/MonthNum) > U.BasePool THEN DATEADD(MM, 1, GETDATE())
                    ELSE  CONVERT(VARCHAR(20),DATEADD(MM,CAST(ROUND((@Basepool - SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)) 
                                /(SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)/@Monthnum),0) as INT), GETDATE()),101)

                    --(basepool - sumcontract) / (sumcontract/monthNum ) is the expected months to reach overage
                END 

            END AS  ExpectedDate
4

2 回答 2

0

您的 @Basepool 变量的值很长,被 INT 溢出。只需尝试以下更正的代码。

declare @Basepool INT =10000
declare @transactioncount bigint =1
declare @Monthnum int=1
declare @ContractId int=1

select CASE @Basepool 
            WHEN 0 THEN 'N/A'
            WHEN -1 THEN 'N/A'
            ELSE CASE 
                    WHEN SUM(SUM(@TransactionCount)) OVER (Partition by @ContractId) > @Basepool THEN 'IN-OVERAGE'
                    ELSE  CONVERT(VARCHAR(20),DATEADD(MM,CAST(ROUND((@Basepool - SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)) 
                                /(SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)/@Monthnum),0) as INT), GETDATE()),101)
                END 

       END AS  ExpectedDate
于 2013-04-29T06:49:50.957 回答
0

首先,我可以添加到 getdate() 的最大月份是

select dateadd(mm, 95840, getdate())

您需要一种方法来处理要添加的月数大于限制的异常情况。

尝试这个:

select DATEADD(MM
    , case when CAST(ROUND((@Basepool - SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId))/(SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)/@Monthnum),0) as INT)
        > datediff(mm, getdate(), '9999-12-31')
        then datediff(mm, getdate(), '9999-12-31')
        else CAST(ROUND((@Basepool - SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId))/(SUM(SUM(@TransactionCount))  OVER (Partition by @ContractId)/@Monthnum),0) as INT)
        end
    , GETDATE())

从查询中取回值后,您需要处理“9999-12-XX”。

于 2013-04-29T06:55:11.470 回答