[英]SQL query select max min value from resultset query
I've got a stored procedure with a query: 我有一个带有查询的存储过程:
SELECT
ID
,AmountInCurrency
,ExtractionDate
FROM
Table
This query returns 2 rows: 该查询返回2行:
ID AmountInCurrency ExtractionDate
1 8.00 2013-10-17 10:00:00.000
1 10.50 2013-09-16 09:00:00.000
I want to select the Min and Max Extrationdate into variables @mindate and @maxdate. 我想将Min和Max Extrationdate选择为变量@mindate和@maxdate。 Right now I put the resultset into a temp table and select the max and min from that. 现在,我将结果集放入临时表中,然后从中选择最大值和最小值。
But is it possible to do it otherwise like in the select: 但是是否有可能像在select中那样:
ID AmountInCurrency ExtractionDate MinExtractionDate MaxExtractionDate
1 8.00 2013-10-17 10:00:00.000 2013-09-16 09:00:00.000 2013-10-17 10:00:00.000
1 10.50 2013-09-16 09:00:00.000 2013-09-16 09:00:00.000 2013-10-17 10:00:00.000
It looks like you want to add the min and max date as extra columns in the result set. 看来您想将最小和最大日期添加为结果集中的额外列。 If so, use the window functions: 如果是这样,请使用窗口函数:
SELECT ID, AmountInCurrency, ExtractionDate,
min(ExtractionDate) over () as MinExtractionDate,
max(ExtractionDate) over () as MaxExtractionDate
FROM Table;
declare @mindate datetime,
@maxdate datetime
select @mindate = min(extraction_date),
@maxdate = max(extraction_date)
from tbl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.