Solved

How to create SQL Query without duplicate records for phonetypes?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now