重复SQL语句的最佳实践

时间:2013-05-10 17:06:45

标签: sql sybase

我正在寻找一种更好的方法来编写高度重复的sql语句。我知道我可以通过使用PHP方法来实现这一点,但我想要一个纯SQL解决方案。我正在使用Sybase。

select
"SvHKeyId"=sh.KeyId,
"Scale"="Partial PTSD",
"ScoreText"=convert(varchar(3),
    (case
        when
            (case
                when
                    1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 1 and 13)
                    and 1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 15 and 21)
                    and 1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 22 and 26)
                then 'Yes'
                else 'No'
            end)
        = 'Yes'
        and
        (case
            when
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=30) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=32) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=33) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=29) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=45) >= 2
                    then 1
                    else 0
                end)
                +
                (case
                    when
                        (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=36) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=44) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=42) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=34) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=35) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) >= 2 then 1 else 0 end)
                    >= 3 then 1
                    else 0
                end)
                +
                (case
                    when
                        (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=40) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=43) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=28) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=39) >= 2 then 1 else 0 end)
                    >= 2 then 1
                    else 0
                end)
            >=2
            then 'Yes'
            else 'No'
        end)
        = 'Yes'
        then 'Yes'
        else 'No'
    end)
    ),
 "ScoreNum"=
    (case
        when
            (case
                when
                    1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 1 and 13)
                    and 1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 15 and 21)
                    and 1 in (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum between 22 and 26)
                then 'Yes'
                else 'No'
            end)
        = 'Yes'
        and
        (case
            when
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=30) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=32) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=33) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=29) >= 2
                        or (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=45) >= 2
                    then 1
                    else 0
                end)
                +
                (case
                    when
                        (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=36) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=44) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=42) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=34) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=35) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) >= 2 then 1 else 0 end)
                    >= 3 then 1
                    else 0
                end)
                +
                (case
                    when
                        (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=40) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=43) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=28) >= 2 then 1 else 0 end)
                        + (case when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=39) >= 2 then 1 else 0 end)
                    >= 2 then 1
                    else 0
                end)
            >=2
            then 'Yes'
            else 'No'
        end)
        = 'Yes'
        then 1
        else 0
    end)
from
    SurvHeader sh
where
    sh.KeyId=105101

这是一个评分程序。我正在比较一系列子分数以得出总分。有更好的SQL方法吗?我想我可以使用存储过程吗?

这是一个更极端的例子,我使用了很多超出限制的子查询:

select
"SvHKeyId"=sh.KeyId,
"Scale"="Total",
"ScoreText"=convert(varchar(3),
       (select sum(Resp) from SurvResp sr where sh.KeyId=sr.SvHKeyId
       and QuestNum in (28,29,30,31,32,33,34,35,36,39,40,42,43,44,45,46) and Resp <> 5)
       + 
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                            else 0
                        end)
                 end)
        end)
        +
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                            else 0
                        end)
                 end)
        end)
        +
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                            else 0
                        end)
                 end)
        end)
    ),
 "ScoreNum"=
       (select sum(Resp) from SurvResp sr where sh.KeyId=sr.SvHKeyId
       and QuestNum in (28,29,30,31,32,33,34,35,36,39,40,42,43,44,45,46) and Resp <> 5)
       + 
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=38) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=37)
                            else 0
                        end)
                 end)
        end)
        +
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=47) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=31)
                            else 0
                        end)
                 end)
        end)
        +
       (case
            when
                (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) <> 5
                and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) <> 5
            then
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        > (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        < (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                        = (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                    then
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                end)
            else
                (case
                    when
                        (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) = 5
                        and (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) = 5
                    then 0
                    else
                        (case
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48)
                            when (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=48) = 5
                            then (select Resp from SurvResp sr where sh.KeyId=sr.SvHKeyId and sr.QuestNum=46)
                            else 0
                        end)
                 end)
        end)
from
    SurvHeader sh
where sh.KeyId=105101

1 个答案:

答案 0 :(得分:2)

这些文字大多数应该是数据,而不是代码。我强烈建议将这些巨型交换机重构为数据,以便更简单的查询可以对其进行评估。然后,“纯SQL”解决方案将更加可行,从长远来看,性能和维护都会更好。也许你可以加入Scoring表并与之比较?

Scoring (Question, Answer, Result)是一种概念上的可能性,希望不要过于简单。

如前所述,"You can write FORTRAN in any language."

即使是T-SQL,似乎也是。

<强>更新

如果这些计算已经完成并且您已在当前事务中更新了表,则无需仅仅因为它们尚未提交而重新执行它们。如果事务中早先更新了此事务,则此事务中的任何查询都将获得此事务的更新数据。

更多关于Sybase ASE transactions

此外,您不应期望GOwork in a stored procedure。它在IDE中用作批处理分隔符,而不是事务控制。