Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting certain records based on priority criteria

Status
Not open for further replies.

douggy

Technical User
Jan 18, 2001
78
0
0
GB
I have two tables:

-Organisation
-Contacts

One organisation can have MNAY contacts.

I want to write a query that will take the company and return only ONE contact based on a given priority.

Eg: take the MD if there is one, if not take the fd, if not take the MK(Marketing Manager).

Can anyone help with this.

Kind regards

Mark
 
Create another table that holds your priorities with a ranking:

EG

Title Rank
MD 1
FD 2
MK 3
……


Next use a nested query, such as:

Code:
SELECT Contacts.name,
       Contacts.jobTitle, 
       Contacts.thisandthat, 
       Contacts.Company, 
       Ranking.Rank
FROM   Ranking INNER JOIN Contacts ON Ranking.Title = Contacts.jobTitle
where  Contacts.Company="B"
and    Ranking.Rank = (
              select max(ranking.rank)
              FROM Ranking INNER JOIN Contacts ON 
                 Ranking.Title = Contacts.jobTitle
              where Contacts.Company = "B"
                       );

Hope This Helps

X-)
Billy H

bhogar@acxiom.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top