我正在从我创建的临时表 #t1 中选择一些行:
SELECT FutContract,
Quantity,
Date,
Price,
TotalQuantity,
PercentOfTotalQ,
CumulativePercentile=
(SELECT ROUND(SUM(PercentOfTotalVol),2) FROM #t1 b
WHERE b.OvernightVol <= a.OvernightVol AND b.FutContract = a.FutContract)
FROM #t1 a
我想再创建两行代表 CumulativePercentile 大于 0.3(第 30 个百分位)的 MIN(Price) 和 MAX(Price),但我能想到的唯一方法是创建另一个临时表。如果可能的话,我宁愿不必这样做。有任何想法吗?
编辑:
;WITH z AS
(SELECT FutContract, OvernightVol, MorningDate, Price, TotalVol,
PercentOfTotalVol, CumulativePercentile=
(SELECT ROUND(SUM(PercentOfTotalVol),2) FROM #t1 b
WHERE b.OvernightVol <= a.OvernightVol AND b.FutContract = a.FutContract) FROM #t1 a)
SELECT *,
(SELECT MIN(Price) OVER(PARTITION BY FutContract) FROM z WHERE CumulativePercentile > 0.3) AS min70px,
(SELECT MAX(Price) OVER(PARTITION BY FutContract) FROM z WHERE CumulativePercentile > 0.3) AS max70px
FROM z