1

作成した一時テーブル #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) を表す行をさらに 2 つ作成したいと考えていますが、考えられる唯一の方法は、別の一時テーブルを作成することです。できれば無い方がいいです。何か案は?

編集:

;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
4

1 に答える 1

4

CTESQL Server 2005 以降を使用している場合は、

;with z as 
(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
)
select 
(select min(price) from z Z1 where Z1.CumulativePercentile > 0.3 and Z1.FutContract = z.FutContract) min_price,
(select max(price) from z Z1 where Z1.CumulativePercentile > 0.3 and Z1.FutContract = z.FutContract) max_price,
*
from z
于 2013-05-16T16:39:13.923 に答える