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 October 22nd, 2007, 06:46 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 SubQueries

I am a bit confussed trying to write a subquery.

I have two tables Project and ProjectAllocationLog

project looks like this
projectID ProjectName
7578 ProjectOne
7579 ProjectTwo
7980 ProjectThree

ProjectAllocationLog looks like this
AllocationLogID projectID UserID
45 7578 10
46 7578 11
49 7579 12
50 7579 13
51 7579 14
53 7578 15
54 7980 16
55 7980 17

I am trying to find the maximum projectAllocationLogID per Project get a grid result as follows

ProjectID AllocationLogID UserID
7578 53 15
7579 51 14
7580 55 17

I have written this

SELECT projectid, userID, AllocationLogID
FROM projects p
WHERE projectid =
(SELECT max(projectAllocationlogid) FROM ProjectAllocationLog as AllocationLogID WHERE P.projectid = ProjectAllocationLog.Projectid);


but I have gone wrong somewhere and I cant work it out

any help appreciated
 
Old October 22nd, 2007, 06:59 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

oo, I have managed to do it, here is the query should anyone wonder

SELECT max(projectAllocationlogid) as AllocationLogid, projectid, NewUserID
FROM projectAllocationLog
WHERE projectid IN (SELECT (projectid) FROM projects)
group by projectid, projectAllocationlogid, NewUserID


:)

 
Old October 22nd, 2007, 07:09 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

Actually, i was wrong, this still doesn't work as I get multiple rows per Projectid



 
Old October 22nd, 2007, 08:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think you were on the right track in both your attempts.

Here's a correlated subquery way:

SELECT projectid, userID, AllocationLogID
FROM projectAllocationLog P1
WHERE AllocationLogID = (SELECT MAX(AllocationLogID ) FROM projectAllocationLog P2 WHERE P2.ProjectID = P1.ProjectID)

Note the table in the FROM clause is the projectAllocationLog and not the Projects table as in your first attempt. If you were to need the projectName or other columns from the Projects table, just JOIN the projectAllocationLog to it on the projectID.

Another way using the IN clause:

SELECT projectid, userID, AllocationLogID
FROM projectAllocationLog
WHERE AllocationLogID in (SELECT MAX(AllocationLogID ) FROM projectAllocationLog GROUP BY projectID)

It's not clear to me which way is more efficient. Looking at the generated query plan for both I was a bit surprised to find the first query looked like it was a bit better. I'm not sure that would hold up if there were a large number of rows in the table, though. YMMV.

As an aside, I think you would be better served having a column which specifically defined what the "maximum" (by which I assume you really mean the "latest") entry for a given project is. I'm guessing your AllocationLogID is an identity column and you are using the fact that these values are increasing to be a surrogate for the time ordering of log entries. It works in this particular case, so there is no arguing with success, but having something like a EntryDate datetime value makes it clear that you are in fact searching for the latest entry rather than the largest ID, which may not be the same thing in every problem domain...

Jeff Mason
[email protected]
 
Old October 22nd, 2007, 09:05 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I was going to suggest:
Code:
SELECT PAL1.projectid, PAL1.MaxLogID, PAL2.userID 
FROM
 (SELECT MAX() MaxLogId, ProjectId
  FROM ProjectAllocationLog
  GROUP BY ProjectId) PAL1
  INNER JOIN ProjectAllocationLog PAL2
  ON PAL1.ProjectId = PAL2.ProjectId;


Not sure that it's any better than Jeff's suggestions though.

--

Joe (Microsoft MVP - XML)
 
Old October 22nd, 2007, 02:04 PM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Many Thanks Jeff and Joe, your help was invaluble to me xx






Similar Threads
Thread Thread Starter Forum Replies Last Post
which is faster Subqueries or Views? lakshminal SQL Language 0 May 1st, 2008 11:20 PM
SubQueries in a select statement in dataset madhusrp Reporting Services 1 February 22nd, 2007 03:03 AM
Multiple subqueries? thf1977 MySQL 1 October 24th, 2006 05:30 PM
Trouble with Subqueries, am I going mad. kim3er SQL Server 2000 3 April 10th, 2005 02:13 PM
subqueries mgdts SQL Server DTS 0 July 28th, 2003 01:13 PM





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