Avatar of garethtnash
garethtnash
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Slightly complicated select statement

Hello SQL Gurus...

I'm pretty sure this is a quick one, but can't for the life of me remember how to do it..

I'm trying to do a select from 3 tables -

dbo.Member (Parent)
dbo.[Member-Phone]
dbo.[Member-Phone]

Select from dbo>member is fine, however as the member can have multiple phone numbers and multiple email addresses...

I can do that fine also..

my code so far is --

SELECT 
M.ID,
M.FirstName, 
M.LastName, 
M.ClubName, 
M.[UK-Member],
M.[Eire-Member],
M.[EU-Member],
M.AccountType, 
M.Birthdate, 
M.Startdate, 
M.URL, 
M.ExitDate,
E.Email,
P.Phone
FROM dbo.Member M
inner join dbo.[Member-Email] E
on E.MemberID = M.ID
inner join dbo.[Member-Phone] P
on P.MemberID = M.ID
LEFT OUTER join dbo.[Member-Phone] MP
on MP.MemberID = M.ID
WHERE M.ID = 12 AND E.defaultemail = 'Y' AND P.DefaultP = 'Y'

Open in new window


Where it is getting complicated is around here --

LEFT OUTER join dbo.[Member-Phone] MP
on MP.MemberID = M.ID

Open in new window


In that what I want to do here is select the top 1 result from dbo.[Member-Phone]  where [MemberID] = dbo.Member.ID AND [Type] = 2 (Mobile).

Taking into consideration that there may not be a record associated, but the select statement should still return results, just with nothing in the MP.Phone colun that will be added to the select statement under P.Phone...

SELECT 
M.ID,
M.FirstName, 
M.LastName, 
M.ClubName, 
M.[UK-Member],
M.[Eire-Member],
M.[EU-Member],
M.AccountType, 
M.Birthdate, 
M.Startdate, 
M.URL, 
M.ExitDate,
E.Email,
P.Phone
MP.Phone  <-HERE

Open in new window


Hope that makes sense?

Thanks
Microsoft SQL Server 2008Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
garethtnash

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jogos

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
garethtnash

ASKER
Great thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck