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

June 18th, 2008, 04:49 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 18th, 2008, 05:05 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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))
|
|

June 18th, 2008, 07:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

June 18th, 2008, 03:25 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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
|
|

June 25th, 2008, 03:49 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank you very much
Debbie x
|
|
 |