Answered by Alex Poole
SET TAB OFF
Question
Oracle/SQL newbie here so please go easy but in need of some help with a 'quirk' in my setup. As an exercise I am creating a company/contact/contactdetails database:
Being my first attempt it's highly likely the code is not sophisticated, elegant, efficient or robust but it appears to work for now and I'm enjoying the learning curve.
So I can check correct working I have created a select statement incorporating full outer table joins, aliases, column formatting and breaks to produce a report to look something like in the link below.
BREAK on c1 on c2 on c3 on c4 on c5 on c6 on c7 on c8 on c9 on c10
COLUMN c1 HEADING ACCT FORMAT A6
COLUMN c2 HEADING COTY FORMAT A4
COLUMN c3 HEADING "COMPANY NAME" FORMAT A35
COLUMN c4 HEADING CITY FORMAT A15
COLUMN c5 HEADING POSTCODE FORMAT A10
COLUMN c6 HEADING FIRSTNAME FORMAT A10
COLUMN c7 HEADING SURNAME FORMAT A10
COLUMN c8 HEADING EMAIL FORMAT A39
COLUMN c9 HEADING TYPE FORMAT A8
COLUMN c10 HEADING NUMBER FORMAT A20
SELECT
cp.code c1, cp.type c2, cp.name c3, ad.city c4, ad.postcode c5, ct.firstname c6, ct.surname c7, em.emailaddress c8, pt.type c9, ph.phoneno c10
FROM company cp FULL OUTER JOIN companyaddress cpad ON cp.companyid=cpad.companyid
FULL OUTER JOIN address ad ON cpad.addressid=ad.addressid
FULL OUTER JOIN contact ct ON cp.companyid=ct.companyid
FULL OUTER JOIN email em ON ct.contactid=em.contactid
FULL OUTER JOIN phone ph ON ct.contactid=ph.contactid
FULL OUTER JOIN phonetype pt ON ph.phonetypeid=pt.phonetypeid
ORDER BY cp.code, cp.type, cp.name, ad.city, ad.postcode, ct.firstname, ct.surname, em.emailaddress, pt.type, ph.phoneno;
2 Issues:
Notice the TYPE column. This shows the type of phone number. What I can't fathom is why sometimes the column is indented to the left and creeps into the formatted space for the EMAIL column. Whatsmore this appears inconsistent. I'm sure this is a formatting issue and I have experimented with returning more/less columns from the joined tables and also with my column size allocation in the column formats. These variations sometimes result in a correct column format but I can't find any consistency which isolates the issue.
Where a contact has two email addresses but only one phone type the phone details are listed twice. See contact Dave Michaels for CompanyE Ltd.
I have tried reducing down the functionality to a few tables and a few lines of code. I hoped this would isolate the problem or limit the lines of code I may need to post but no joy. I can supply the schema diagram and scripts for table create, data insert and select to anyone who would be willing to assist me. I would be very grateful.
Solution
Answered by Alex Poole
SET TAB OFF