Solved

How to create SQL Query without duplicate records for phonetypes?

Posted on 2008-10-31
3
536 Views
Last Modified: 2013-11-16
I have a phonenumber_table with two fields: phonetype and phonenumber. The different phone types are business, fax, etc. When I join this table with my company_table and select phonenumber, I get duplicates because of the different phone types. How can I get both fax number and phone number in the layout (phonenumber as Phone_Number, phonenumber as Fax_Number) whichout duplicate records? My current query is below. Thanks.

select company,phonetype, phonenumber as Phone_Number, phonenumber as Fax_Number, status
from company_table
inner join phonenumber_table on company_table.ID = phonenumber_table.ObjOwner_ID
where status = 'Active'


0
Comment
Question by:asmyatt
[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
3 Comments
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 22853934
use aliases
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22853963
You need to join to the table for each type.
select c.company, p.phonenumber as Phone_Number, fax.phonenumber as Fax_Number, status
from company_table c
  left outer join phonenumber_table F
   on c.ID = phonenumber_table.ObjOwner_ID
   and f.phoneType = 'FAX'
  left outer join phonenumber_table p
   on c.ID = phonenumber_table.ObjOwner_ID
   and p.phoneType = 'PHONE'
 
where status = 'Active'

Open in new window

0
 
LVL 2

Author Closing Comment

by:asmyatt
ID: 31512203
Thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

734 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