all groups > sql server misc > september 2007 >
You're in the

sql server misc

group:

SQL - how to query for highest valued data in row



SQL - how to query for highest valued data in row Walter Cohen
9/1/2007 8:38:44 PM
sql server misc: Hello.
I have rows in a database that are grouped by a key value. They all have
the same key value but some other columns on each row are not valued the
same. How can I construct a query that will just return one row of the set
with the highest value for one of the columns?

Here are some of the rows of data in the database. The columns are
Parent_id, Page_num, external_id, index1, index2, index3, and name. The
rows I am interested in will all have the same Parent_id (10316). In the
sample data I did a query for rows where page_num > 1 (which is why you see
rows from 2 - 17). What I'd like to do is be able to return one row with
the highest page_num value (17).

Thanks.
Walter

Sample Data:
Parent_id Page_num external_id index1 index2 index3 name
10316 17 11542 K40397-1 4184840 8/14/2007 1756715
10316 16 11474 K40397-1 4184840 8/14/2007 1756715
10316 15 11473 K40397-1 4184840 8/14/2007 1756715
10316 14 11268 K40397-1 4184840 8/14/2007 1756715
10316 13 11142 K40397-1 4184840 8/14/2007 1756715
10316 12 11111 K40397-1 4184840 8/14/2007 1756715
10316 11 11110 K40397-1 4184840 8/14/2007 1756715
10316 10 11109 K40397-1 4184840 8/14/2007 1756715
10316 9 11108 K40397-1 4184840 8/14/2007 1756715
10316 8 11050 K40397-1 4184840 8/14/2007 1756715
10316 7 11043 K40397-1 4184840 8/14/2007 1756715
10316 6 11042 K40397-1 4184840 8/14/2007 1756715
10316 5 10994 K40397-1 4184840 8/14/2007 1756715
10316 4 10986 K40397-1 4184840 8/14/2007 1756715
10316 3 10985 K40397-1 4184840 8/14/2007 1756715
10316 2 10984 K40397-1 4184840 8/14/2007 1756715

Re: SQL - how to query for highest valued data in row Ed Murphy
9/2/2007 10:23:02 AM
[quoted text, click to view]

select parent_id, max(page_num)
from the_table
Re: SQL - how to query for highest valued data in row Walter Cohen
9/5/2007 7:16:04 AM
Thank you!

Walter
[quoted text, click to view]
AddThis Social Bookmark Button