ORACLE KEEP Function

  • Hi there,

    Is there any equivalent function for this in SQL 2005 SQL? I need to DENSE_RANK() a column based on the first (max)value of each TOP N paritioned group, so that I can sort the group result set by this value.

    In Oracle I use the following

    SELECT

    SELECT Group_id,

    DName,

    MaxReads,

    TopN,

    MAX (MaxReads) KEEP (dense_rank first ORDER BY topn) over (PARTITION BY Group_ID) maxRank

    FROM (TOP N Query)

  • There is not a KEEP function in SQL Server. You would need to do something like this:

    create table #t (n int)

    insert into #t (n) values (100)

    insert into #t (n) values (200)

    insert into #t (n) values (300)

    insert into #t (n) values (300)

    select n

    from

    (select n,

    dense_rank() over(

    order by cnt desc) as

    rnk

    from

    (select n,

    count(*) as

    cnt

    from #t

    group by n)

    x)

    y

    where rnk = 1

    Which I got from here[/url].

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply