这是我正在操作的单个表的片段。该表包含总共15个不同的符号,每天跨越3年(CurrDate)和大约370分钟(PRMinute)。我正在尝试一个查询,它会显示每个符号的最低值,每天显示低点(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]
答案 0 :(得分:1)
在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;
这假定prlow
包含有关最低值的信息。没有名为value
的列。
答案 1 :(得分:0)
这与戈登的答案类似,但不使用排名功能。
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]