0
我有以下查询:计算添加百分位数(或四分位数),以现有的SQL查询
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;
那我试图25Percentile添加到上面的SQL。下面是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应产生为以类似于峰度是在所述第一查询中计算出的方式,第一查询创建子集的百分计算(即,在数据的子集)。上面为25Percentile提供的SQL只是计算所有数据。
这必须在MS Access 2013中工作。我有一个超级慢的VBA答案,因此更喜欢纯粹的SQL答案。
第一个查询完全按照我希望它工作的方式工作,除了我需要包含25Percentile计算。
编辑: 如果你感到困惑,第一个查询以这种方式被使用,但我并没有包括所有的,因为我认为这将只是分散:
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(第一四分位数),但它不像GrtIs集成那样集合在一起,就像Kurtosis一样,它为所有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
EDIT(最快!):这将得到四分/百分度和峰度快两倍,(我想它,我收到了正确的后回答我以前的方法):
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));
仅供参考 - 我发现了一个方法来获得四分位数已经是我做这件事的速度了。看起来我最后的编辑(刚刚添加它)。 – mountainclimber