Solved

How to create SQL Query without duplicate records for phonetypes?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

777 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