有人可以帮我简化/加速这个功能吗?

时间:2011-01-07 17:15:07

标签: sql-server vb.net sql-server-2005

该功能根据每个月的设定值计算保险费。

下面是我昨晚想出来的。我知道它很脏而且很糟糕,但它是我用现有知识做的最好的。

declare @january decimal(6,3)
declare @february decimal(6,3)
declare @march decimal(6,3)
declare @april decimal(6,3)
declare @may decimal(6,3)
declare @june decimal(6,3)
declare @july decimal(6,3)
declare @august decimal(6,3)
declare @september decimal(6,3)
declare @october decimal(6,3)
declare @november decimal(6,3)
declare @december decimal(6,3)

set @january = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'january')
set @february = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'february')
set @march = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'march')
set @april = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'april')
set @may = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'may')
set @june = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'june')
set @july = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'july')
set @august = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'august')
set @september = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'september')
set @october = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'october')
set @november = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'november')
set @december = (select earningvalue from tblearningpatterns where clientname = @client and earningpattern = @pattern and calmonth = 'december')

set @tripearnings = 1 - @pretripearnings


if @triptype = '1'
    begin
        if datepart(month, @outdate) = 1
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@january /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@january + @february) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@january + @february + @march)    / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@january + @february + @march + @april + @may + @june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 2
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@February /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@february + @march) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@february + @march + @April)  / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@february + @march + @april + @may + @june + @july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 3
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@march /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@march + @april) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@march + @April + @may)   / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@march + @april + @may + @june + @july + @august + @september + @october + @november + @december + @January)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 4
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@april /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@april + @may) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@April + @may + @june)    / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@april + @may + @june + @july + @august + @september + @october + @november + @december + @January + @February)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end 
        if datepart(month, @outdate) = 5
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@may /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@may + @june) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@may + @june + @july) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@may + @june + @july + @august + @september + @october + @november + @december + @January + @February + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end 
        if datepart(month, @outdate) = 6
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@june /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@june + @july) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@june + @july + @august)  / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@june + @july + @august + @september + @october + @november + @december + @January + @February + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 7
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@july /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@july + @august) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@july + @august + @september) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@july + @august + @september + @october)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@july + @august + @september + @october + @november + @december + @January + @February + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 8
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@august /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@august + @september) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@august + @september + @october)  / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@august + @september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@august + @september + @october + @november + @december + @January + @February + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 9
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@september /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@september + @october) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@september + @october + @november)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@september + @october + @november + @december)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @january + @february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@september + @october + @november + @december + @January + @February + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 10
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@october /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@october + @november) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@october + @november + @december) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@october + @november + @december + @january)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@october + @november + @december + @january + @february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@october + @november + @december + @January + @February + @march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 11
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@november /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@november + @december) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@november + @december + @january) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@november + @december + @january + @february)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@november + @december + @january + @february + @march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@november + @december + @January + @February + @march + @april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
        if datepart(month, @outdate) = 12
            begin
                if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 1
                    set @earnedpremium = @premium * (@december /100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 2
                    set @earnedpremium = @premium * ((@december + @january) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 3
                    set @earnedpremium = @premium * ((@december + @january + @february) / 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 4
                    set @earnedpremium = @premium * ((@december + @january + @february + @march)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 5
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 6
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april + @may)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 7
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april + @may + @june)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 8
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 9
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april + @may + @june + @july)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 10
                    set @earnedpremium = @premium * ((@december + @january + @february + @march + @april + @may + @june + @july + @august)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 11
                    set @earnedpremium = @premium * ((@december + @January + @February + @march + @april + @may + @june + @july + @august + @september)/ 100)
                else if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
                    set @earnedpremium = @premium
            end
    end

1 个答案:

答案 0 :(得分:3)

试试这个

set @tripearnings = 1 - @pretripearnings

if @triptype = '1'
begin
    if round((datediff(day, @outdate, @experiencedate) / 30.41),0) = 12
        set @earnedpremium = @premium
    else
    begin
        ;with cte as (
            -- this cte generates data for all 12 months, and then copies them again to make months 13-24
            -- the "months" 13-24 make calculation easier, because whe you ask for Aug to Apr the next year
            -- the range is simply months 8 to 16
            select mth=itr+month('1 ' + calmonth + ' 2010'), earningvalue
            from tblearningpatterns, (select itr=0 union all select 12) x
            where clientname = @client and earningpattern = @pattern and isdate('1 ' + calmonth + ' 2010')=1
        )
        select @earnedpremium = @premium * (
            (
            select sum(earningvalue)
            from cte
            where mth between month(@outdate) and month(@outdate)+round((datediff(day, @outdate, @experiencedate) / 30.41),0)-1
            ) / 100)
    end
end