Results 1 to 6 of 6

Thread: [RESOLVED] Need a little help with SQL query

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [RESOLVED] Need a little help with SQL query

    Is there a way to get the values in all the columns of a row when I use the max() function on one of the columns? ie. if I have a column in my table called "index" & I do select max(index) from myTable it just returns the value in that column. I need all the values in the row where index is "max". What's the easiest way to do this? Thanks...

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Need a little help with SQL query

    A sub qerury like this:

    Select * From myTable where Index = (select max(index) From mytable)

    I do hope that index is not the name of the field in the database.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Need a little help with SQL query

    OK, that worked. Thanks. Now lets try something a little different. Suppose I want to select the row where 'index' is max from among the rows where 'order_num' = something. For example, in my table I have 10 rows where 'order_num' is the same value, now I want to get the 1 row of those 10 where 'index' is max. I hope I explained that well enough.

    And, no, my column name is not 'index', I just used that as an example. But I gather that using 'index' is bad practice?

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Need a little help with SQL query

    Yes index is a Reserved word and all reserved words need to be avoided like the plague when naming fields.

    What I think you are asking is like this:
    Select * From myTable where Index = (select max(index) From mytable Where order_num = Something here)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Need a little help with SQL query

    Thanks, that worked perfect...

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Need a little help with SQL query

    If that resolves you issued could you please use the thread tools and mark the issuse resolved.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width