缩短重复的SQL查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了缩短重复的SQL查询相关的知识,希望对你有一定的参考价值。

是否有更好的方法来编写以下代码,也许有某种循环,等等。在此之前,我在查询中创建了一个临时表。然后,我尝试创建不同的列,这些列使我可以按时间顺序在一行中对客户帐户进行的操作。

SELECT 
    a.*,
    Action_1 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '1' )),
    Action_1_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '1' )),
    Action_2 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '2' )),
    Action_2_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '2' )),
    Action_3 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '3' )),
    Action_3_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '3' )),
    Action_4 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '4' )),
    Action_4_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '4' )),
    Action_5 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '5' )),
    Action_5_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '5' )),
    Action_6 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '6' )),
    Action_6_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '6' )),
    Action_7 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '7' )),
    Action_7_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '7' )),
    Action_8 = (SELECT Rating_Changed_To FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '8' )),
    Action_8_Time = (SELECT Change_Date FROM #TempICHist WHERE (IC_ID_18 = a.ICID_18 AND Rank = '8' ))
FROM
    #TempICs a
ORDER BY
    a.Programme,a.IC_Created_Date

我希望这个问题很清楚,但是如果有混淆,我可以答复。

答案

对于每个ID,您需要将8条记录投影到16个不同的属性中,因此您的sql不会变得更加紧凑。

但是,您可以使用左联接代替相关子查询,这可以提高性能。但是,请检查两个查询的rdbms解释计划(这将告诉您优化程序将查询转换成的原语)。 SELECT a.*, h1.Rating_Changed_To Action_1, h2.Rating_Changed_To Action_2, h3.Rating_Changed_To Action_3, h4.Rating_Changed_To Action_4, h5.Rating_Changed_To Action_5, h6.Rating_Changed_To Action_6, h7.Rating_Changed_To Action_7, h8.Rating_Changed_To Action_8, h1.Change_Date Action_1_Time, h2.Change_Date Action_2_Time, h3.Change_Date Action_3_Time, h4.Change_Date Action_4_Time, h5.Change_Date Action_5_Time, h6.Change_Date Action_6_Time, h7.Change_Date Action_7_Time, h8.Change_Date Action_8_Time, FROM #TempICs a LEFT JOIN #TempICHist h1 ON ( h1.C_ID_18 = a.ICID_18 AND Rank = '1' ) LEFT JOIN #TempICHist h2 ON ( h2.C_ID_18 = a.ICID_18 AND Rank = '2' ) LEFT JOIN #TempICHist h3 ON ( h3.C_ID_18 = a.ICID_18 AND Rank = '3' ) LEFT JOIN #TempICHist h4 ON ( h4.C_ID_18 = a.ICID_18 AND Rank = '4' ) LEFT JOIN #TempICHist h5 ON ( h5.C_ID_18 = a.ICID_18 AND Rank = '5' ) LEFT JOIN #TempICHist h6 ON ( h6.C_ID_18 = a.ICID_18 AND Rank = '6' ) LEFT JOIN #TempICHist h7 ON ( h7.C_ID_18 = a.ICID_18 AND Rank = '7' ) LEFT JOIN #TempICHist h8 ON ( h8.C_ID_18 = a.ICID_18 AND Rank = '8' ) ORDER BY a.Programme,a.IC_Created_Date ;

另一答案
一种方法是将apply

条件

以上是关于缩短重复的SQL查询的主要内容,如果未能解决你的问题,请参考以下文章

mybatis-基于xml的多表查询

有啥办法可以缩短这个 SQL 查询

mysql : 需要缩短查询

Microsoft SQL Server 代码片段收集

如何解决 SQL Server 死锁 - 一旦更改顺序和缩短查询用尽?

sql [SQL查询片段]用于在命令行或通过R和其他工具使用SQL的快速代码段#tags:sql,R,text processing,命令li