2013-04-29 1 views
1

문제점은 무엇인지 알지만 누구든지이 쿼리를 해결하는 방법으로 제안하십시오. 내가 값을 얻고있다 DATEADD에 달 동안 계산할 때dateadd에서 개월 값을 처리하는 방법

문제는 이상 98,540 :(

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

쿼리가 2013-04-29에 실행될 경우 98540 대신 95840이어야합니다. –

답변

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'을 처리해야합니다.

+0

고마워요. 이것은 내 문제를 해결 :) –

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

그게 문제라는 것을 알고 있습니다. 베이스 풀 값을 10000000으로 설정해야합니다. 데이터를 변경할 수 없습니다. 나는 코드를 바꿔야 만한다. –

+0

나는 몇 분만 줘 보겠다. – Anvesh

+0

나는 최대 95841 개월 만 추가 할 수 있습니다. – Anvesh

관련 문제