有什么办法可以使嵌套案例语句的SQL代码更干净?

时间:2019-11-14 17:07:14

标签: sql sql-server

我有一个查询,我根据一个可以做10个不同选项的变量来做稍微不同的事情。

 (
        CaCartons
        + 
        case when PgFeet is null or PgWeight is null then 0 else
            COALESCE(

                case when CLIENTNAME ='01' then
                    case
                        when PgWeight < @CartonMaxWeight and PgFeet < @CartonMaxVolume01
                            then 1
                        when PgWeight / @CartonMaxWeight >= PgFeet / @CartonMaxVolume01
                            then PgWeight / @CartonMaxWeight
                        else
                            PgFeet / @CartonMaxVolume01
                    end
                else null end,

                case when CLIENTNAME ='02' then
                    case
                        when PgWeight < @CartonMaxWeight and PgFeet < @CartonMaxVolume02
                            then 1
                        when PgWeight / @CartonMaxWeight >= PgFeet / @CartonMaxVolume02
                            then PgWeight / @CartonMaxWeight
                        else
                            PgFeet / @CartonMaxVolume02
                    end
                else null end,

                case when CLIENTNAME ='03' then
                    case
                        when PgWeight < @CartonMaxWeight and PgFeet < @CartonMaxVolume03
                            then 1
                        when PgWeight / @CartonMaxWeight >= PgFeet / @CartonMaxVolume03
                            then PgWeight / @CartonMaxWeight
                        else
                            PgFeet / @CartonMaxVolume03
                    end
                else null end
            )
        end
    ) TotalCartons

此操作将继续提供10个CLIENTNAMES

有什么方法可以使此代码更简洁,更易于阅读?

2 个答案:

答案 0 :(得分:1)

我将在FROM子句中添加更多逻辑。我认为这是您想要的逻辑:

select . . .,
       (CaCartons +
        (case when PgFeet is null or PgWeight is null then 0 
              when PgWeight < @CartonMaxWeight and PgFeet < v.CartonMaxVolume01
              then 1
              when PgWeight / @CartonMaxWeight >= PgFeet / v.CartonMaxVolume01
              then PgWeight / @CartonMaxWeight
              else PgFeet / v.CartonMaxVolume01
         end)
        )
from t left join
     (values ('client01', @CartonMaxVolume01),
             ('client02', @CartonMaxVolume02),
             . . .
     ) v(client, CartonMaxVolume)
     on t.client = v.client

答案 1 :(得分:0)

要在查询的多个位置重构和重用表达式,您可以将其提升到Common Table Expression中,然后从中进行SELECT。例如:

with q as
(
   select *, 
   case when CLIENTNAME ='01' then @CartonMaxVolume01
        when CLIENTNAME ='02' then @CartonMaxVolume02
        when CLIENTNAME ='03' then @CartonMaxVolume03
        else null end ClientCartonMaxVolume
   from ...
)
select 
        CaCartons
        + 
        case when PgFeet is null or PgWeight is null then 0 
             when PgWeight < @CartonMaxWeight and PgFeet < ClientCartonMaxVolume then 1
             when PgWeight / @CartonMaxWeight >= PgFeet / ClientCartonMaxVolume
             else  PgFeet / ClientCartonMaxVolume end  as TotalCartons
from q;