如何从2个不同的视图中将2列连接在一起?

时间:2012-10-24 15:57:36

标签: sql oracle

使用oracle SQL开发人员,

我有两个视图有不同的列。如何将视图A中的第1列连接到视图B?我想将AR_INVOICE_INQ_V.CLI_NAME CLI_NAME从视图B加入到视图A中。

查看A

CREATE OR REPLACE FORCE VIEW "KPRD"."AC_INPUT_AUDIT" ("CLIENT", "SEQUENCE_CODE", "EMPLOYEE", "ORIGINAL_DATE", "HOURS_SPENT", "LONG_DESCRIPTION", "TIME_AT_ACTUAL", "MANAGER")
AS
  SELECT CLIENT,
    SEQUENCE_CODE,
    EMPLOYEE,
    ORIGINAL_DATE,
    HOURS_SPENT,
    LONG_DESCRIPTION,
    TIME_AT_ACTUAL,
    MANAGER
  FROM TIME_DETAILS
  WHERE MANAGER BETWEEN 'AAGG' AND 'AZJE'
  AND ORIGINAL_DATE    > '28/SEP/2012'
  AND TIME_AT_ACTUAL ! = 0;

查看B

CREATE OR REPLACE FORCE VIEW "KPRD"."AC_AR_INV_BAL" ("CLIENT", "CLI_NAME", "PARTNER", "PAR_NAME", "MANAGER", "REFERENCE", "STATUS_CODE", "INVOICE_DATE", "TIME_BILLED", "GROUP_CLIENT_CODE", "GRPCLI_NAME", "INVOICE_BALANCE")
AS
  SELECT AR_INVOICE_INQ_V.CLIENT CLIENT,
    AR_INVOICE_INQ_V.CLI_NAME CLI_NAME,
    AR_INVOICE_INQ_V.PARTNER PARTNER,
    AR_INVOICE_INQ_V.PAR_NAME PAR_NAME,
    AR_INVOICE_INQ_V.MANAGER MANAGER,
    AR_INVOICE_INQ_V.REFERENCE REFERENCE,
    AR_INVOICE_INQ_V.STATUS_CODE STATUS_CODE,
    AR_INVOICE_INQ_V.INVOICE_DATE INVOICE_DATE,
    AR_INVOICE_INQ_V.TIME_BILLED TIME_BILLED,
    AR_INVOICE_INQ_V.GROUP_CLIENT_CODE GROUP_CLIENT_CODE,
    AR_INVOICE_INQ_V.GRPCLI_NAME GRPCLI_NAME,
    AR_INVOICE_INQ_V.INVOICE_BALANCE
  FROM AR_INVOICE_INQ_V
  WHERE MANAGER BETWEEN 'AAGG' AND 'AZGG';

2 个答案:

答案 0 :(得分:0)

为了将第1列从视图A加入视图B,两个表或视图都必须包含第1列。如果要将AR_INVOICE_INQ_V.CLI_NAME CLI_NAME从视图B加入视图A,则视图A必须包含该列CLI_NAME。否则,您无法基于CLI_NAME列进行连接。

答案 1 :(得分:0)

加入视图的语法与表格相同。我不完全确定您要实现的目标,但我认为您希望查看A中的所有列以及查看B中的另一列。假设CLIENT在语法上都有一个共同的值,那就是:

SELECT AIA.CLIENT,
    AIA.SEQUENCE_CODE,
    AIA.EMPLOYEE,
    AIA.ORIGINAL_DATE,
    AIA.HOURS_SPENT,
    AIA.LONG_DESCRIPTION,
    AIA.TIME_AT_ACTUAL,
    AIA.MANAGER,
    AAIB.CLI_NAME
FROM AC_INPUT_AUDIT AIA
JOIN AC_AR_INV_BAL AAIB ON AAIB.CLIENT = AIA.CLIENT;

这似乎假设两个视图中的记录之间存在一对一的关系,这似乎不太可能,特别是因为MANAGER过滤器不同。因此,您可能会得到一组不同的结果,可能会出现重复和/或缺失记录。外部联接可能会解决这个问题,但也可能会使情况变得更糟,因此您需要更清楚地了解视图的相关性以及您要实现的目标。

如果您有权访问基础表,您可能会发现加入这些表的性能更高,但您可能需要以两种方式进行测试。