微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有变量效率低下的 SQL Server 双 case 语句

如何解决带有变量效率低下的 SQL Server 双 case 语句

这可能只是不知道要搜索什么的情况,因此具有该方向的答案是完全可以接受的。我正在将数据推送到我的 sql 服务器并具有以下(模拟),它可以工作,但似乎效率很低。我最终更愿意消除我所有的变量。

--From PHP
declare @employeeid varchar(25) = '100971'
declare @reviewscore as tinyint = 52
declare @payclass as varchar(25) = 'salary management'
--/From PHP

declare @wagequartile as tinyint = (select quartile from activeemployees where EmployeeID= @employeeid)

declare @scorequartile as tinyint = (
select
    case
        when @reviewscore <= max1 then 1
        when @reviewscore <= max2 then 2
        when @reviewscore <= max3 then 3
        else 4
    end as scoreQuartile
from ReviewscoreMatrix where PayClass=@payclass
)

select
    case
        when @scorequartile = 1 then scoreQuartile1
        when @scorequartile = 2 then scoreQuartile2
        when @scorequartile = 3 then scoreQuartile3
        else 4
    end as PercentRaise
from RaisePercentMatrix
where WageQuartile = @wagequartile

我尝试使用 CTE,但无法弄清楚如何使它变得更好。相关部分:

with cte1 as (
    select
        case
            when @reviewscore <= max1 then 1
            when @reviewscore <= max2 then 2
            when @reviewscore <= max3 then 3
            else 4
        end as [scoreQuartile]
    from ReviewscoreMatrix where PayClass=@payclass
)
select
    case
        when (select top 1 scoreQuartile from cte1) = 1 then scoreQuartile1
        when (select top 1 scoreQuartile from cte1) = 2 then scoreQuartile2
        when (select top 1 scoreQuartile from cte1) = 3 then scoreQuartile3
        else 4
    end as PercentRaise
from RaisePercentMatrix
where WageQuartile = @wagequartile

我觉得我在 CTE 上走在正确的轨道上,但嵌入 cte1 中的选择似乎是那里的失误。任何帮助表示赞赏。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。