SQL - 选择最小值并返回该min的所有列信息

时间:2015-06-29 23:56:25

标签: sql-server group-by min

我正在使用SQL Server 2014 Management Studio

为了完整,我将显示完整的查询:

SELECT 
    final_eqid.eqId, eqPrice.date_, 
    optContract.expDate, optContract.strike,
    MIN(ivListed.ivMid) iv
FROM final_eqid 
    INNER JOIN eqPrice ON final_eqid.eqId = eqPrice.eqId 
    INNER JOIN optContract ON eqPrice.eqId = optContract.eqId 
    INNER JOIN ivListed ON eqPrice.date_ = ivListed.date_ 
        AND optContract.optId = ivListed.optId
WHERE 
    eqPrice.date_ = '2008-01-02' 
    AND final_eqid.eqid = 7
GROUP BY final_eqid.eqid, eqPrice.date_, optContract.expDate, optContract.strike

我故意只选择了一个date_和一个eqid,但我每个都有数千个。

结果如下:

 eqId   date_                expDate           strike   iv
    7   2008-01-02 00:00:00 2008-01-19 00:00:00 25.000  0.9319
    7   2008-01-02 00:00:00 2008-02-16 00:00:00 25.000  0.6306
    7   2008-01-02 00:00:00 2008-05-17 00:00:00 25.000  0.5349
    7   2008-01-02 00:00:00 2008-08-16 00:00:00 25.000  0.5006
    7   2008-01-02 00:00:00 2008-01-19 00:00:00 30.000  0.5727
    7   2008-01-02 00:00:00 2008-02-16 00:00:00 30.000  0.5083
    7   2008-01-02 00:00:00 2008-05-17 00:00:00 30.000  0.2752
    7   2008-01-02 00:00:00 2008-08-16 00:00:00 30.000  0.4559
    7   2008-01-02 00:00:00 2008-01-19 00:00:00 35.000  0.3854
    7   2008-01-02 00:00:00 2008-02-16 00:00:00 35.000  0.4065
    7   2008-01-02 00:00:00 2008-05-17 00:00:00 35.000  0.4308
    7   2008-01-02 00:00:00 2008-08-16 00:00:00 35.000  0.4213
    7   2008-01-02 00:00:00 2008-01-19 00:00:00 40.000  0.3472
    7   2008-01-02 00:00:00 2008-02-16 00:00:00 40.000  0.37
    7   2008-01-02 00:00:00 2008-05-17 00:00:00 40.000  0.3969
    7   2008-01-02 00:00:00 2008-08-16 00:00:00 40.000  0.3953

我想要获得的是min iv strike以及该行的所有其他信息。在上面的数据中,这将理想地返回

eqId    date_   expDate strike  iv
7   1/2/2008    1/19/2008   40  0.3472
7   1/2/2008    2/16/2008   40  0.37
7   1/2/2008    5/17/2008   30  0.2752
7   1/2/2008    8/16/2008   40  0.3953

如果我不按罢工进行分组,那么我会得到正确的数字,但却无法识别具有最小(iv)值的罢工

提前感谢您的任何帮助

1 个答案:

答案 0 :(得分:2)

我认为您想要的是使用Common Table Expression(CTE),它指定您可以查询的临时结果集。 MIN函数与OVER子句一起使用,将FROM子句生成的结果集划分为应用该函数的分区。

这是一个基于你的查询的例子(虽然我没有测试过,所以需要注意):

WITH cteData AS
(
    SELECT final_eqid.eqId, 
           eqPrice.date_, 
           optContract.expDate, 
           optContract.strike,  
           ivListed.ivMid iv, 
           MIN(ivListed.ivMid) OVER (PARTITION BY final_eqid.eqId, eqPrice.date_,optContract.expDate) as MinIV
    FROM   final_eqid INNER JOIN
                 eqPrice ON final_eqid.eqId = eqPrice.eqId INNER JOIN
                 optContract ON eqPrice.eqId = optContract.eqId INNER JOIN
                 ivListed ON eqPrice.date_ = ivListed.date_ AND optContract.optId = ivListed.optId
    WHERE eqPrice.date_ ='2008-01-02' and final_eqid.eqid=7
    GROUP BY final_eqid.eqid, eqPrice.date_, optContract.expDate, optContract.strike, ivListed.ivMid
)
select eqId, date_, expDate, strike, iv
from cteData
where cteData.iv=cteData.MinIV