Solved

How to create SQL Query without duplicate records for phonetypes?

Posted on 2008-10-31
3
538 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

623 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