Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SQL - how to query for highest valued data in row

2 views
Skip to first unread message

Walter Cohen

unread,
Sep 1, 2007, 8:38:44 PM9/1/07
to
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


Ed Murphy

unread,
Sep 2, 2007, 1:23:02 PM9/2/07
to
Walter Cohen wrote:

> 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).

select parent_id, max(page_num)
from the_table
group by parent_id

Walter Cohen

unread,
Sep 5, 2007, 7:16:04 AM9/5/07
to
Thank you!

Walter
"Ed Murphy" <emur...@socal.rr.com> wrote in message
news:46daf1aa$0$6385$4c36...@roadrunner.com...

0 new messages