Wrox Programmer Forums
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 23rd, 2007, 07:33 AM
Registered User
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with SQL Statement

I have a table PageContent which has the following columns: ContentID (contains the unique id of each page content), Content (contains the page content), Version (a number indicating the version of Content). Since several verions of each ContentID exist, ContentID is repeated in several rows. The Content field is unique for each ContentID but is not necessarily unique across the whole table.

I want to do a SELECT statement that will return ContentID and Content but only show the most recent version of each ContentID. SELECT DISTINCT works for returning each unique ContentID number but since the Content fields are different, I can't use a DISTINCT statement.

Sorry, this is probably pretty simple.
Old August 23rd, 2007, 08:08 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

Just out of curiosity, what is the primary key for this table?

Assuming by using the words "..the most recent version.." you mean the row with the maximum value of the version column for a given ContentID, then a correlated subquery will select those rows:

SELECT ContentID, Content
FROM PageContent P1
WHERE Version = (SELECT MAX(Version) FROM PageContent P2 WHERE P1.ContentID = P2.ContentID)

Jeff Mason

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL Statement??? carswelljr SQL Server 2000 2 August 25th, 2006 12:40 PM
SQL Statement rylemer SQL Language 3 October 21st, 2005 09:07 PM
Sql Statement help morpheus Classic ASP Basics 0 March 9th, 2004 10:55 AM
sql statement chrisangus@btinternet.com Beginning VB 6 2 June 23rd, 2003 04:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.