SQL - 选择最小值并返回该最小值的所有列信息
SQL - selecting min value and returning all column information for that 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
我要获取的是最小静脉注射次数和该行的所有其他信息。在上面的数据中,这将理想地返回
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
如果我不按罢工分组,那么我会得到正确的数字,但无法识别具有 min(iv) 值的罢工
提前感谢您的帮助
我想你想要的是使用 Common Table Expression (CTE), which specifies a temporary result set that you can query on. The MIN function used with an 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
我正在使用 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
我要获取的是最小静脉注射次数和该行的所有其他信息。在上面的数据中,这将理想地返回
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
如果我不按罢工分组,那么我会得到正确的数字,但无法识别具有 min(iv) 值的罢工
提前感谢您的帮助
我想你想要的是使用 Common Table Expression (CTE), which specifies a temporary result set that you can query on. The MIN function used with an 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