Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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
  #1 (permalink)  
Old January 5th, 2005, 06:38 PM
Registered User
 
Join Date: Jan 2005
Location: Singapore, , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Just how do you do it? Complicated!

The Tables

MTL Table

--------------------------------------------------------------------------------

NRIC RANK NAME
8401234A PTE John
8321321B SGT Harry
3213121Z BG Dick

--------------------------------------------------------------------------------

Next of Kins Table

--------------------------------------------------------------------------------

MTL_NRIC Name Relationship
8401234A ROse Mother
8401234A Jack Father
8321321B Elaine Spouse

--------------------------------------------------------------------------------

I'm trying to return ALL data from each personnel in MTL, whether they have a Next of Kin or not, and if they do not have a Next of Kin, just return a blank field for that column.

In case they have 2 Next of Kins, just return 1 row for that personnel with any 1 Next of Kin.


Below is the SQL code I tried really hard to finally come up with ..

--------------------------------------------------------------------------------

SELECT MTL.NRIC, MTL.Rank, MTL.Name
FROM MTL, (SELECT DISTINCT m.NRIC FROM MTL as m LEFT OUTER JOIN [Next of Kins] ON (m.NRIC = [Next of Kins].MTL_NRIC)) AS o, Ranking
WHERE (MTL.NRIC = o.m.NRIC) AND (Ranking.[Rank Name] Like MTL.[Rank]) AND (MTL.ORD > Now()) AND (MTL.PostedOut <> TRUE)
ORDER BY Ranking.[Rank Class], MTL.Name;

--------------------------------------------------------------------------------

It works but .. does not have any Next of Kin info..

--------------------------------------------------------------------------------

SELECT MTL.NRIC, MTL.Rank, MTL.Name, o.[Next of Kins].Name, o.[Next of Kins].Relationship
FROM MTL, (SELECT DISTINCT m.NRIC FROM MTL as m LEFT OUTER JOIN [Next of Kins] ON (m.NRIC = [Next of Kins].MTL_NRIC)) AS o, Ranking
WHERE (MTL.NRIC = o.m.NRIC) AND (Ranking.[Rank Name] Like MTL.[Rank]) AND (MTL.ORD > Now()) AND (MTL.PostedOut <> TRUE)
ORDER BY Ranking.[Rank Class], MTL.Name;

--------------------------------------------------------------------------------

This has an error as o.[Next of Kins].???? does not return any data as the table 'o' does not SELECT it.
But if I do SELECT them (NOK.Name, NOK.Relationship), it would also include duplicate NRIC in the results, as PTE John has 2 Next of Kins..


Please help.

Yours,
Going Grazy


  #2 (permalink)  
Old February 2nd, 2005, 07:52 PM
Authorized User
 
Join Date: Jun 2003
Location: Califon, NJ, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just noticed this unanswered post and hope it's not too late to be of help...

First, the MTL table is fine, but the NextOfKin table needs a primary key that is unique. Because there are two records having the same value for MTL_NRIC, that field won't work for the table's primary key. So I inserted as the first field a new AutoNumber field called pk_MTL_NRIC, and set it to the primary key of the table.

With that change, the following code returns the result set you're looking for:

SELECT MTL.*,
       NextOfKin.*
FROM MTL
LEFT JOIN NextOfKin
  ON MTL.NRIC = NextOfKin.MTL_NRIC;

Here's the result set:

NRIC Rank MTL.Name pk_NextOfKin MTL_NRIC NextOfKin.Name Relationship
3213121Z BG Dick
8321321B SGT Harry 3 8321321B Elaine Spouse
8401234A PTE John 1 8401234A Rose Mother
8401234A PTE John 2 8401234A Jack Father

Hope this helps.

Pete


Similar Threads
Thread Thread Starter Forum Replies Last Post
Complicated Insert Statement jackie25 SQL Server ASP 1 May 31st, 2006 09:08 AM
Complicated Indexes luciano XSLT 0 July 19th, 2005 08:01 PM
complicated query rajanikrishna Classic ASP Basics 4 June 17th, 2004 04:17 PM
Complicated Query roniestein Access 3 December 13th, 2003 10:34 AM
Too Complicated shiraz BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 23rd, 2003 10:06 PM





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