How to get the max value of a column?

  • Hi,

    I wanted to find the max value of a column. I have the following table structure

    OrderIDTaskIDSerialNo

    112

    123

    211

    222

    From the above table i want the following result for every orderID and TaskID display the max value

    SerialNo

    2

    3

    1

    2

    By using MAX i am getting the OrderID and TaskID as well which is not my intended result. I am using SQL Server. How would i get the intended result?

    Thanks,

    Rajagopalan

  • What's your expected result?

  • rajagopalanseeth (6/29/2015)


    Hi,

    I wanted to find the max value of a column. I have the following table structure

    OrderIDTaskIDSerialNo

    112

    123

    211

    222

    From the above table i want the following result for every orderID and TaskID display the max value

    SerialNo

    2

    3

    1

    2

    By using MAX i am getting the OrderID and TaskID as well which is not my intended result. I am using SQL Server. How would i get the intended result?

    Thanks,

    Rajagopalan

    Sounds like you're looking for something like this:

    SELECT OrderID, TaskID, MAX(SerialNo)

    FROM yourTable

    GROUP BY OrderID, TaskID

    ORDER BY OrderID, TaskID

    Remove OrderID and/or TaskID from the SELECT if you don't want them displayed, but leave them in the GROUP BY.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • you can try below this query :

    SELECT OrderId, TaskID, MAX(SerialNo)SerialNo

    FROM table_name

    GROUP BY OrderId, TaskId

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • kapil_kk (7/1/2015)


    you can try below this query :

    SELECT OrderId, TaskID, MAX(SerialNo)SerialNo

    FROM table_name

    GROUP BY OrderId, TaskId

    Isn't that what I already said?



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

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