Home All Groups Group Topic Archive Search About
Author
16 Feb 2006 9:46 PM
jrmann1999
I have a Join that's creating identical rows except for an incremental
column.  I want to only return the row with the highest incremental
number, how can I accomplish this?

DB structure is like

EP_KEY(INT), PAT_KEY(INT), PAT_NAME(VARCHAR), EPISODE(INT)

The EP_KEY and PAT_KEY are the primary key, the EPISODE is the
incremental column, data might look like:

1    1    John Doe      1
1    1    John Doe      2
1    1    John Doe      3


I only want to return

1    1    John Doe      3

Author
16 Feb 2006 10:15 PM
Anith Sen
Not sure what the key columns are. Anyway, do:

SELECT *
  FROM tbl t1
WHERE t1.episode = ( SELECT MAX( t2.episode )
                        FROM tbl t2
                       WHERE t2.pat_name = t1.pat_name ) ;

--
Anith
Author
16 Feb 2006 11:27 PM
Hugo Kornelis
On 16 Feb 2006 13:46:10 -0800, jrmann1***@gmail.com wrote:

Show quote
>I have a Join that's creating identical rows except for an incremental
>column.  I want to only return the row with the highest incremental
>number, how can I accomplish this?
>
>DB structure is like
>
>EP_KEY(INT), PAT_KEY(INT), PAT_NAME(VARCHAR), EPISODE(INT)
>
>The EP_KEY and PAT_KEY are the primary key, the EPISODE is the
>incremental column, data might look like:
>
>1    1    John Doe      1
>1    1    John Doe      2
>1    1    John Doe      3
>
>
>I only want to return
>
>1    1    John Doe      3

Hi jrmann1999,

SELECT   EP_Key, Pat_Key, Pat_Name, MAX(Episode)
FROM     (insert your query here)
GROUP BY EP_Key, Pat_Key, Pat_Name

--
Hugo Kornelis, SQL Server MVP

AddThis Social Bookmark Button