将百分位数(或四分位数)计算添加到现有SQL查询

时间:2015-10-04 19:35:54

标签: sql ms-access

我有以下查询:

SELECT  "Cc EV PS" AS factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #8/14/2015# AS calcdate, 
        (Sum((t1.[Cc EV PS]-z.TheAvg)^4)/Count(t1.[Cc EV PS]))/(Sum((t1.[Cc EV PS]-z.TheAvg)^2)/Count(t1.[Cc EV PS]))^2 AS Kurtosis
FROM tbl_DatedModel_2015_0929_0 AS t1 
   INNER JOIN
            (SELECT t2.[GICS Sector], Avg(t2.[Cc EV PS]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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.[Cc EV PS]) > 0;

我试图在上面的SQL中添加25Percentile。这是25Prercentile的SQL:

SELECT TOP 1 0.75*( SELECT Max(tp2.[Cc EV PS]) 
                    FROM tbl_DatedModel_2015_0929_0 AS tp2
                    WHERE tp2.[Cc EV PS] IN 
                        (SELECT TOP 25 PERCENT tp3.[Cc EV PS] 
                        FROM tbl_DatedModel_2015_0929_0 AS tp3 
                        WHERE tp3.[Cc EV PS] Is Not Null 
                        ORDER BY tp3.[Cc EV PS])) + 0.25*
                            (SELECT Min(tp4.[Cc EV PS]) 
                            FROM tbl_DatedModel_2015_0929_0 AS tp4
                            WHERE tp4.[Cc EV PS] IN 
                                (SELECT TOP 75 PERCENT tp5.[Cc EV PS] 
                                FROM tbl_DatedModel_2015_0929_0 AS tp5
                                WHERE tp5.[Cc EV PS] Is Not Null 
                                ORDER BY tp5.[Cc EV PS] DESC)) AS 25Percentile
FROM tbl_DatedModel_2015_0929_0 AS tp1;

最后,25Percentile应该为在第一个查询中创建的子集产生百分位计算,类似于在第一个查询中计算Kurtosis的方式(即,在数据的子集上)。上面为25Percentile提供的SQL只计算所有数据。

这必须在MS Access 2013中有效。我的VBA答案非常慢,因此更喜欢纯SQL答案。

第一个查询完全按照我希望的方式工作,除了我需要包含25Percentile calc。

修改 如果您感到困惑,第一个查询就会以这种方式使用,但我没有包含所有这些,因为我认为这会分散注意力:

SELECT  "Cc EV PS" AS factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #8/14/2015# AS calcdate, 
        (Sum((t1.[Cc EV PS]-z.TheAvg)^4)/Count(t1.[Cc EV PS]))/(Sum((t1.[Cc EV PS]-z.TheAvg)^2)/Count(t1.[Cc EV PS]))^2 AS Kurtosis
FROM tbl_DatedModel_2015_0929_0 AS t1 
   INNER JOIN
            (SELECT t2.[GICS Sector], Avg(t2.[Cc EV PS]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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.[Cc EV PS]) > 0
UNION ALL
SELECT  "USD Market Cap" AS factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #8/14/2015# AS calcdate, 
        (Sum((t1.[USD Market Cap]-z.TheAvg)^4)/Count(t1.[USD Market Cap]))/(Sum((t1.[USD Market Cap]-z.TheAvg)^2)/Count(t1.[USD Market Cap]))^2 AS Kurtosis
FROM tbl_DatedModel_2015_0929_0 AS t1 
   INNER JOIN
            (SELECT t2.[GICS Sector], Avg(t2.[USD Market Cap]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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.[USD Market Cap]) > 0
UNION ALL SELECT    "IU Mkt Cap" AS factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #8/14/2015# AS calcdate, 
        (Sum((t1.[IU Mkt Cap]-z.TheAvg)^4)/Count(t1.[IU Mkt Cap]))/(Sum((t1.[IU Mkt Cap]-z.TheAvg)^2)/Count(t1.[IU Mkt Cap]))^2 AS Kurtosis
FROM tbl_DatedModel_2015_0929_0 AS t1 
   INNER JOIN
            (SELECT t2.[GICS Sector], Avg(t2.[IU Mkt Cap]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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 Mkt Cap]) > 0;

编辑:这包括25Percentile(第一个四分位数),但它不像Grtosis那样在GICS扇区聚合,它为所有GICS扇区产生相同的值。它应该为到达子集(GICS扇区)产生一个答案:

SELECT  "Cc EV PS" AS factor, 
        "GICS Sector/" & t1.[GICS Sector], 
        #8/14/2015# AS calcdate, 
        (Sum((t1.[Cc EV PS]-z.TheAvg)^4)/Count(t1.[Cc EV PS]))/(Sum((t1.[Cc EV PS]-z.TheAvg)^2)/Count(t1.[Cc EV PS]))^2 AS Kurtosis,
         0.75*(SELECT Max(tp2.[Cc EV PS]) 
                FROM tbl_DatedModel_2015_0929_0 AS tp2
                WHERE tp2.[Cc EV PS] IN 
                    (SELECT TOP 25 PERCENT tp3.[Cc EV PS] 
                    FROM tbl_DatedModel_2015_0929_0 AS tp3 
                    WHERE tp3.[Cc EV PS] Is Not Null 
                    ORDER BY tp3.[Cc EV PS])) + 0.25*
                        (SELECT Min(tp4.[Cc EV PS]) 
                        FROM tbl_DatedModel_2015_0929_0 AS tp4
                        WHERE tp4.[Cc EV PS] IN 
                            (SELECT TOP 75 PERCENT tp5.[Cc EV PS] 
                            FROM tbl_DatedModel_2015_0929_0 AS tp5
                            WHERE tp5.[Cc EV PS] Is Not Null 
                            ORDER BY tp5.[Cc EV PS] DESC)) AS 25Percentile
FROM tbl_DatedModel_2015_0929_0 AS t1 
   INNER JOIN
            (SELECT t2.[GICS Sector], Avg(t2.[Cc EV PS]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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.[Cc EV PS]) > 0

编辑(最快!):这将使四分位数/百分位数和Kurtosis的速度提高两倍(我在收到前一种方法的正确答案后得知):

SELECT "Cc EV PS" AS factor, "GICS Sector/" & t1.[GICS Sector] AS Expr1,
    #8/14/2015# AS calcdate, 
 (Sum((t1.[Cc EV PS]-z.TheAvg)^4)/Count(t1.[Cc EV PS]))/(Sum((t1.[Cc EV PS]-z.TheAvg)^2)/Count(t1.[Cc EV PS]))^2 AS Kurtosis, 
            (select max([Cc EV PS]) from tbl_DatedModel_2015_0929_0 where tbl_DatedModel_2015_0929_0.[Cc EV PS] in 
(select top 25 percent [Cc EV PS] from tbl_DatedModel_2015_0929_0 
where t1.[GICS Sector] = tbl_DatedModel_2015_0929_0.[GICS Sector] order by [Cc EV PS])) AS 25Percentile 
FROM tbl_DatedModel_2015_0929_0 AS t1 INNER JOIN (SELECT t2.[GICS Sector], Avg(t2.[Cc EV PS]) AS TheAvg 
            FROM tbl_DatedModel_2015_0929_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.[Cc EV PS]))>0));

1 个答案:

答案 0 :(得分:1)

我不确定MS Access,但这些替换应该可以解决问题:

(?<=pointa)pattern(?=pointb)