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 June 18th, 2008, 04:49 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default Subquery

if I have a table of Projects (projectid, projectname) and another table of projectperiods (Projectid, Period, from, to). a project can have many periods.

eg
PROJECTS
projectid, projectname
101 The Little House Project
102 The big House project

PROJECTPERIODS
Periodid, Projectid, period, from, To
1 101 1 01/01/08 01/03/08
2 102 1 01/01/08 30/04/08
3 101 2 02/03/08 30/05/08
4 101 3 01/06/08 30/07/08

I want to be able to select projectid and project name from projects and the maximum period from projectperiods.

In order to do this i have made a view out of the project periods grouping by the projectid, selecting the max of the period.

then creating another view and joining the two.

I am not too sure of SQL, but I think i can do this with one view using a subquery, so i would select projectid, projectname, max(project) from project .... and then I get confussed. can anyone help me please, or point me to a tutorial of some sort explaining this.

cheers debbie


 
Old June 18th, 2008, 05:05 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

i am playing with this, but it doesn't seem to work

SELECT *projectid
FROM dbo.PROJECT
WHERE (ProjectID =
                          (SELECT MAX(period)
                            FROM projectperiod
                            WHERE projectid = project.projectid))

 
Old June 18th, 2008, 07:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
SELECT
  P.ProjectId,
  P.ProjectName,
  PP.PeriodId,
  PP.Period,
  PP.[From],
  PP.[To]
FROM
  Projects P INNER JOIN ProjectPeriods PP
  ON P.ProjectId = PP.ProjectId INNER JOIN
  (SELECT ProjectId, MAX(Period) MaxPeriod FROM ProjectPeriods GROUP BY ProjectId) MP
  ON P.ProjectId = MP.ProjectId AND PP.Period = MP.MaxPeriod;
--

Joe (Microsoft MVP - XML)
 
Old June 18th, 2008, 03:25 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

As a minor comment...

If you don't need all the fields from ProjectPeriods that joefawcett provided in his answer--if you indeed need only the fields you noted in your first message ("I want to be able to select projectid and project name from projects and the maximum period from projectperiods"), then you can simplify that query a bit:
Code:
SELECT P.ProjectId, P.ProjectName, MP.MaxPeriod
FROM Projects P,
     ( SELECT ProjectId, MAX(Period) AS MaxPeriod 
       FROM ProjectPeriods 
       GROUP BY ProjectId
     ) AS MP
WHERE P.ProjectId = MP.ProjectId
 
Old June 25th, 2008, 03:49 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Thank you very much

Debbie x






Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble with subquery ghall202 Access 1 July 9th, 2008 01:55 PM
Subquery as table brettk_1 Oracle 1 January 22nd, 2007 02:10 PM
Do i have to use a subquery, if yes then how? code_lover SQL Language 2 January 2nd, 2007 02:22 PM
Help...subquery problem Twistdmojo Classic ASP Databases 4 September 14th, 2006 11:45 AM
subquery khansa MySQL 1 February 21st, 2006 02:49 AM





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