获得第一次出现的行

时间:2016-04-01 08:54:07

标签: sql-server tsql

我有一个由以下t-sql语句创建的表:

CREATE TABLE [Core].[PriceHistory](
    [PriceHistoryId] [bigint] IDENTITY(1,1) NOT NULL,
    [SourceId] [tinyint] NOT NULL,
    [SymbolId] [smallint] NOT NULL,
    [Ask] [real] NOT NULL,
    [Bid] [real] NOT NULL,
    [TickTime] [bigint] NOT NULL,
    [ModifiedDate] [datetime2](3) NOT NULL,
    [Direction] [tinyint] NULL,
CONSTRAINT [PK_PriceHistory] PRIMARY KEY CLUSTERED 
(
[PriceHistoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

假设我有一个SymbolIds列表,例如(1,2,3)。

我希望得到具有ModifiedDate的SymbolId的第一行是>比'2016-04-01 00:00:00'

1 个答案:

答案 0 :(得分:2)

SELECT *
FROM (
    SELECT DENSE_RANK() OVER (
            PARTITION BY SymbolId ORDER BY ModifiedDate
            ) RNK
        ,*
    FROM PriceHistory
    WHERE ModifiedDate > '2014-04-01 00:00:00'
    ) T
WHERE RNK = 1