Is there a way to find TOP X records with grouped data?
Posted
by Chris Simmons
on Stack Overflow
See other posts from Stack Overflow
or by Chris Simmons
Published on 2010-06-01T22:07:23Z
Indexed on
2010/06/01
22:13 UTC
Read the original article
Hit count: 171
I'm working with a Sybase 12.5 server and I have a table defined as such:
CREATE TABLE SomeTable(
[GroupID] [int] NOT NULL,
[DateStamp] [datetime] NOT NULL,
[SomeName] varchar(100),
PRIMARY KEY CLUSTERED (GroupID,DateStamp)
)
I want to be able to list, per [GroupID], only the latest X records by [DateStamp]. The kicker is X > 1, so plain old MAX() won't cut it. I'm assuming there's a wonderfully nasty way to do this with cursors and what-not, but I'm wondering if there is a simpler way without that stuff.
I know I'm missing something blatantly obvious and I'm gonna kick myself for not getting it, but .... I'm not getting it. Please help.
Is there a way to find TOP X records, but with grouped data?
© Stack Overflow or respective owner