You must Sign In to post a response.
  • Category: .NET

    How to Get Top values for this Query


    Create Table TestDemo
    ID int,
    amount decimal(6,2)

    Insert into TestDemo values(10,1500)
    Insert into TestDemo values(9,1000)
    Insert into TestDemo values(9,NULL)
    Insert into TestDemo values(8,50)
    Insert into TestDemo values(7,30)
    Insert into TestDemo values(6,10)

    select * from TestDemo

    -- I need how to get top 2 records only 2 separate variable in sql server
    --like this

    -- in my sql server declare value this

    ---Declare @TOP1 int ,@Top2 int
    --I need set value here
    -- set @TOP1=1500 -- for id = 10
    -- set @TOP2=1000 --- for id = 9 is not null
    -- How will do this
  • #768510

    You want only first two values is that what you mean?

    Select Top 2 Amount From TestDemo ORDER BY ID DESC

    If this is not you expecting. Kindly give some nice examples.


  • #768516
    You can use given code snippet for Get Top values from any table
    SELECT TOP 1, emp.salary from ( SELECT TOP N, emp.salary
    FROM emp
    ORDER BY emp.salary DESC )

    Emp is table name
    Salary and name is field of table
    And Nth gives top values from your table


  • Sign In to post your comments