Home > Mobile >  How to get least value from table in sql
How to get least value from table in sql

Time:01-12

I am getting the top value and least value from table,table accepting the nulls also.i am able to get the top value but not the low value.

This is my table data:

PostBidId UserId   PostId        BidAmount
-------------------------------------------------
1691       159       1239   
1684       147       1239        100000000000000
1683       147       1239        12345666666
1680       147       1239        6777777
1682       147       1239        900
1681       147       1239        90000000

My top value is 100000000000000, low value id 900.

This is my code:

SELECT TOP(1) 
    BidAmount AS HighestBid, '0' AS LowestBid 
FROM 
    [dbo].[Bids] 
WHERE
    PostId = 1239 AND BidAmount != '' 
ORDER BY
    BidAmount ASC

Output is: 100000000000000

SELECT
    MIN (BidAmount) AS LowestBid, '0' AS HighestBid 
FROM 
    [dbo].[Bids] 
WHERE
    PostId = 1239 AND BidAmount != '' 

Output is: 100000000000000, what I am expecting is: 900.

CodePudding user response:

    SELECT MIN (CAST (BidAmount as bigint)) AS LowestBid, '0' AS HighestBid
    FROM [dbo].[Bids] 
    WHERE PostId = 1239 AND BidAmount != ''
  •  Tags:  
  • Related