Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 16th, 2006, 09:46 AM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed with JOIN Query

Hi Group,

My apologies for the lengthy post, but here goes...

I have the following tables:

TABLE Vehicles
(
[ID] nvarchar(5),
[Make] nvarchar(20),
[Model] nvarchar(20),
)

TABLE [Vehicle Status]
(
[ID] int, /* this is an auto-incrementing field*/
[Vehicle ID] nvarchar(5), /* foriegn key, references Vehicles.[ID] */
[Status] nvarchar(20),
[Status Date] datetime
)

Here's my problem...

I have the following data in my [Vehicles] and [Vehicle Status] tables:

[ID] [Make] [Model]
----------------------
H80 Nissan Skyline
H86 Toyota Aristo


[ID] [Vehicle ID] [Status] [Status Date]
----------------------------------------
1 H80 OK 2006-10-01
2 H80 Damage 2006-10-05
3 H86 OK 2006-10-13
4 H86 Dent 2006-10-15
5 H86 Scratched 2006-10-16

I need a query that will join the two tables so that the most recent
status of each vehicle can be determined. I've gotten as far as:

SELECT Vehicle.[ID], Make, Model, [Status], [Status Date] FROM
[Vehicles] INNER JOIN [Vehicle Status] ON [Vehicles].[ID] = [Vehicle
Status].[Vehicle ID]

Of course this produces the following results:

[ID] [Make] [Model] [Status] [Status Date]
--------------------------------------------
H80 Nissan Skyline OK 2006-10-01
H80 Nissan Skyline Damage 2006-10-05
H86 Toyota Aristo OK 2006-10-13
H86 Toyota Aristo Dent 2006-10-15
H86 Toyota Aristo Scratched 2006-10-16

How do I filter these results so that I get only the MOST RECENT vehicle
status?

i.e:

[ID] [Make] [Model] [Status] [Status Date]
--------------------------------------------
H80 Nissan Skyline Damage 2006-10-05
H86 Toyota Aristo Scratched 2006-10-16

Thanks in advance,
Rommel the iCeMAn


Rommel the iCeMAn
Barbados, Carribean
 
Old October 16th, 2006, 10:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

This database is really an Access database, huh? ;)

You are going to eventually regret those table and column names that contain embedded spaces...

Anyway, let's build up this query a piece at a time, sort of from the inside out.

The first question is how to determine the maximum date for any given [Vehicle ID]. That would be:
Code:
SELECT MAX([Status Date])
  FROM [Vehicle Status]
 WHERE [Vehicle ID] = <the id you want>
then, we would select the row that has this [Status Date] value, again, for the vehicle ID in question:
Code:
SELECT *
  FROM [Vehicle Status]
 WHERE [Vehicle ID] = <the id you want>
  AND [Status Date] = <the maxiumum we just found above>
These can be combined using the first query as a subquery of the second:
Code:
SELECT *
  FROM [Vehicle Status]
 WHERE [Vehicle ID] = <the id you want>
  AND [Status Date] = (SELECT MAX([Status Date])
                         FROM [Vehicle Status]
                        WHERE [Vehicle ID] = <the id you want>)
Of course, you want to do this for ALL the vehicle IDs, not just one at a time, so we'll want to execute the above for each Vehicle ID in the table. This is exactly what a "correlated subquery" is designed to do:
Code:
SELECT Vehicles.[ID], Make, Model, [Status], [Status Date]
  FROM [Vehicles]
 INNER JOIN [Vehicle Status] VS ON [Vehicles].[ID] = VS.[Vehicle ID]
 WHERE [Status Date] = (SELECT MAX([Status Date])
                          FROM [Vehicle Status]
                         WHERE [Vehicle Status].[Vehicle ID] = VS.[Vehicle ID])
Note that since we refer to the [Vehicle Status] table twice in the query, once in the JOIN and once in the subquery, we need to distinguish the two references from each other. Thus, we use an alias of the table, VS, in the FROM clause.

Also, this alias tell the subquery to use the value of the [Vehicle ID] from the outer query as the selection predicate in the WHERE clause in the subquery. This is the "correlated" part of a "correlated subquery": the subquery is executed once for each row of the outer query, using the current values of the row being processed.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 16th, 2006, 11:01 AM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow! Thanks Jeff! I can see I have some more SQL to learn :-)

"You are going to eventually regret those table and column names that contain embedded spaces..."

It's actually an SQL Server db, I thought using the spaces were ok once I embed them in square brackets. The only problem I have encountered so far occurred when I was generating a database script. The SQL script contained some syntax errors because the relationships which referenced tables whose names contained embedded spaces were not enclosed in square brackets. Are there more problems I should be aware of?

Thanks again for the help :-)

Rommel the iCeMAn
Barbados, Carribean
 
Old October 16th, 2006, 11:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are correct that using embedded spaces is OK if you surround the identifiers containing them in brackets. I'm not aware of any specific problems when you do so.

Everything is supposed to work just fine.

Famous last words.

The use of brackets, though, is NOT SQL Standard. The SQL Standard requires that identifiers be enclosed in quotes. This is worse, in my opinion, as all those quote characters really can make reading a query source rather difficult.

YMMV.

I questioned whether the database was Access because defining identifiers with embedded spaces was rather encouraged (it made user friendly column names for reporting easy).

I've heard it said that an SQL programmer can take over a year to recover from Access poisoning...


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
self join query error Aneri Visual Studio 2005 0 March 1st, 2007 05:35 AM
Help needed, inner join? grstad SQL Language 2 April 12th, 2006 07:18 AM
Nested Query or Join? rlull SQL Server 2000 4 November 10th, 2005 05:23 PM
JOIN query wverner Access 2 April 5th, 2005 01:44 PM
Join Query tsimsha Classic ASP Databases 2 December 2nd, 2004 09:31 AM





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