[英]SQL Server - Find Minimum Value by Minute within Day Grouped by Symbol
Here is a snippet of the single table I am operating on. 这是我正在操作的单个表的片段。 The table contains a total of 15 different symbols, spanning 3 years (CurrDate) and approximately 370 Minutes (PRMinute) in each day.
该表总共包含15个不同的符号,跨越3年(CurrDate),每天大约370分钟(PRMinute)。 I am attempting a query that will show me the lowest value for each symbol, each day and display the minute (PRMinute) that low took place.
我正在尝试执行一个查询,该查询每天向我显示每个符号的最小值,并显示发生低位的分钟(PRMinute)。
Symbol CurrDate PRMinute PROpen PRHigh PRLow PRClose
SLV 2013-07-09 1 18.55 18.56 18.55 18.555
TLT 2013-07-09 1 107.45 107.45 107.45 107.45
UCO 2013-07-09 1 34.10 34.10 34.10 34.10
!COMP 2013-07-10 1 3502.11 3503.41 3502.00 3503.17
!DJI 2013-07-10 1 15298.03 15315.86 15297.07
!GSPC 2013-07-10 1 1651.56 1652.21 1651.56 1652.09 0
!RUT 2013-07-10 1 1017.69 1017.87 1017.69 1017.87 0
CREATE TABLE [dbo].[IntradayInput](
[Symbol] [varchar](10) NULL,
[CurrDate] [date] NULL,
[PRMinute] [smallint] NULL,
[PROpen] [money] NULL,
[PRHigh] [money] NULL,
[PRLow] [money] NULL,
[PRClose] [money] NULL,
[PRVol] [bigint] NULL
) ON [PRIMARY]
The canonical way of doing this in SQL Server is to use row_number()
: 在SQL Server中执行此操作的规范方法是使用
row_number()
:
select ii.*
from (select ii.*,
row_number() over (partition by symbol, currdate
order by prlow asc end) as seqnum
from IntradayInput ii
) ii
where seqnum = 1;
This assumes that prlow
contains the information on the lowest value. 假定
prlow
包含有关prlow
的信息。 There is no column called value
. 没有称为
value
列。
This is similar to Gordon's answer, but is does not use a ranking function. 这类似于戈登的答案,但不使用排名功能。
SELECT [Symbol], [CurrDate], MIN([PRLow]) AS Min_PRLow
, (SELECT MIN([PRMinute]) FROM [IntradayInput] mi
WHERE mi.[Symbol]=i.[Symbol] AND mi.[CurrDate]=i.[CurrDate]
AND mi.PRLow=MIN(i.[PRLow])) AS PRMinute
FROM [IntradayInput] i GROUP BY [Symbol],[CurrDate]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.