Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create SQL Query without duplicate records for phonetypes?

Posted on 2008-10-31
3
Medium Priority
?
545 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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