Solved

Slightly complicated select statement

Posted on 2012-03-12
2
385 Views
Last Modified: 2012-03-12
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
0
Comment
Question by:garethtnash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 37709411
If you want 2 rows returned when there are 2 mobile phones or there is always only one mobile phone then just adding the
 AND MP.[Type] = 2
at the end of your WHERE is enough

When you only want max 1 mobile phone and there could be multiple then

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,
(select top 1 mp.Phone 
 from dbo.[Member-Phone] MP
 WHERE MP.MemberID = M.ID and mp.type = 2
 ) as Mobile
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
WHERE M.ID = 12 AND E.defaultemail = 'Y' AND P.DefaultP = 'Y'

Open in new window

0
 

Author Closing Comment

by:garethtnash
ID: 37709416
Great thanks
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question