优化sql查询,仅限MS Access

时间:2015-10-07 04:53:24

标签: sql ms-access

我想优化此查询:

SELECT "IU" AS Factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #10/6/2015# AS CalcDate, 
        (Count(t1.[IU]) / Count(*) - 1)*100 AS NaPct, 
        Avg(t1.[IU]) AS Mean, 
        StDev(t1.[IU]) AS Sd, 
        Min(t1.[IU]) AS Low, 
         (SELECT Max(tp1.[IU]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[IU] IN 
             (SELECT top 25 percent tp2.[IU] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[IU])) AS 25Percentile, 
         (SELECT Max(tp1.[IU]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[IU] IN 
             (SELECT top 50 percent tp2.[IU] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[IU])) AS Median, 
         (SELECT Max(tp1.[IU]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[IU] IN 
             (SELECT top 75 percent tp2.[IU] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[IU])) AS Q3, 
        Max(t1.[IU]) AS High, 
         ((SELECT Max(tp1.[IU]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[IU] IN 
             (SELECT top 75 percent tp2.[IU] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[IU])) - 
         (SELECT Max(tp1.[IU]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[IU] IN 
             (SELECT top 25 percent tp2.[IU] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[IU]))) AS IQR, 
        IIf((Sum((t1.[IU]-z.TheAvg)^2)/Count(t1.[IU])) = 0 OR Count(t1.[IU]) = 0,NULL,Sum((t1.[IU]-z.TheAvg)^4)/Count(t1.[IU]))/(Sum((t1.[IU]-z.TheAvg)^2)/Count(t1.[IU]))^2 AS Kurtosis, 
        IIf(Count(t1.[IU]) = 0 OR (Sum((t1.[IU]-z.TheAvg)^2)/Count(t1.[IU]))^1.5 = 0,NULL,(Sum((t1.[IU]-z.TheAvg)^3)/Count(t1.[IU]))/(Sum((t1.[IU]-z.TheAvg)^2)/Count(t1.[IU]))^1.5) AS Skewness, 
        Count(t1.[IU]) AS Obs 
FROM tbl_DatedModel_2015_1005_0 AS t1 
        INNER JOIN 
                  (SELECT t2.[GICS Sector], Avg(t2.[IU]) AS TheAvg 
                  FROM tbl_DatedModel_2015_1005_0 AS t2 
                  GROUP BY t2.[GICS Sector]) AS z ON t1.[GICS Sector] = z.[GICS Sector] 
GROUP BY t1.[GICS Sector] 
HAVING Count(t1.[IU]) > 0 
UNION ALL 
SELECT "GM" AS Factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #10/6/2015# AS CalcDate, 
        (Count(t1.[GM]) / Count(*) - 1)*100 AS NaPct, 
        Avg(t1.[GM]) AS Mean, 
        StDev(t1.[GM]) AS Sd, 
        Min(t1.[GM]) AS Low, 
         (SELECT Max(tp1.[GM]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[GM] IN 
             (SELECT top 25 percent tp2.[GM] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[GM])) AS 25Percentile, 
         (SELECT Max(tp1.[GM]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[GM] IN 
             (SELECT top 50 percent tp2.[GM] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[GM])) AS Median, 
         (SELECT Max(tp1.[GM]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[GM] IN 
             (SELECT top 75 percent tp2.[GM] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[GM])) AS Q3, 
        Max(t1.[GM]) AS High, 
         ((SELECT Max(tp1.[GM]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[GM] IN 
             (SELECT top 75 percent tp2.[GM] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[GM])) - 
         (SELECT Max(tp1.[GM]) 
         FROM tbl_DatedModel_2015_1005_0 AS tp1 
         WHERE tp1.[GM] IN 
             (SELECT top 25 percent tp2.[GM] 
             FROM tbl_DatedModel_2015_1005_0 AS tp2 
             WHERE t1.[GICS Sector] = tp2.[GICS Sector] ORDER BY tp2.[GM]))) AS IQR, 
        IIf((Sum((t1.[GM]-z.TheAvg)^2)/Count(t1.[GM])) = 0 OR Count(t1.[GM]) = 0,NULL,Sum((t1.[GM]-z.TheAvg)^4)/Count(t1.[GM]))/(Sum((t1.[GM]-z.TheAvg)^2)/Count(t1.[GM]))^2 AS Kurtosis, 
        IIf(Count(t1.[GM]) = 0 OR (Sum((t1.[GM]-z.TheAvg)^2)/Count(t1.[GM]))^1.5 = 0,NULL,(Sum((t1.[GM]-z.TheAvg)^3)/Count(t1.[GM]))/(Sum((t1.[GM]-z.TheAvg)^2)/Count(t1.[GM]))^1.5) AS Skewness, 
        Count(t1.[GM]) AS Obs 
FROM tbl_DatedModel_2015_1005_0 AS t1 
        INNER JOIN 
                  (SELECT t2.[GICS Sector], Avg(t2.[GM]) AS TheAvg 
                  FROM tbl_DatedModel_2015_1005_0 AS t2 
                  GROUP BY t2.[GICS Sector]) AS z ON t1.[GICS Sector] = z.[GICS Sector] 
GROUP BY t1.[GICS Sector] 
HAVING Count(t1.[GM]) > 0

注意:

  • 除了计算的值之外,UNION ALL的上方和下方是相同的。在上面的代码中,计算的值是IU和GM。
  • 这只是示例代码。实际上,有几个UNION ALL语句,但为简单起见,这里省略了它们。该查询是在VBA中动态构建的。
  • 我无法删除计算,但我愿意修改/简化它们。
  • 我认为一种简单的优化方法是为GICS Sector"带来一个指数。对于WHERE语句。关于这方面的确认和SQL示例对我有用。
  • 这必须在MS Access 2013中有效。
  • 所有SQL。即请不要VBA。
  • 虽然首选SQL答案,但欢迎使用概念性的想法。
  • 我发现一些优化是有限的,因为你不了解我的数据结构。
  • 我的查询中没有任何内容是ID(此时)。
  • 我读过这个:https://support.microsoft.com/en-us/kb/209126

0 个答案:

没有答案