简体   繁体   English

SQL,Oracle,列格式

[英]SQL, Oracle, column formatting

Oracle/SQL newbie here so please go easy but in need of some help with a 'quirk' in my setup. Oracle / SQL新手,所以请轻松一些,但在我的设置中需要一些“怪异”帮助。 As an exercise I am creating a company/contact/contactdetails database: 作为练习,我将创建公司/联系人/ contactdetails数据库:

  • 9 tables total 总共9张桌子
  • Company can be a supplier, customer or both 公司可以是供应商,客户或两者都可以
  • Company can have many or no addresses (billing/delivery etc) 公司可以有很多地址或没有地址(开单/送货等)
  • Company can have many or no contact names 公司可以有很多或没有联系人姓名
  • Contacts can have many or no email addresses and many or no phone numbers of different types (DDI, mobile, fax etc) 联系人可以有很多电子邮件地址,也可以没有电子邮件地址,也可以有不同类型的电话号码(DDI,移动电话,传真等)

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. 因此,我可以检查是否正确工作,我创建了一个select语句,其中包含完整的外部表连接,别名,列格式和中断,以生成报告,如下所示。

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;

Link to Output image 链接到输出图像

2 Issues: 2期:

  1. Notice the TYPE column. 注意TYPE列。 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. 我无法理解的是为什么有时该列向左缩进并爬入EMAIL列的格式化空间。 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. 这些变化有时会导致列格式正确,但是我找不到任何可以隔离问题的一致性。

  2. 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. 请参阅Dave Michaels与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. 我会很感激。

由亚历克斯·普尔回答

SET TAB OFF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM