Oracle SQL 加入两个 tables/views 避免交叉积

Oracle SQL Join two tables/views avoid cross product

我有两个 tables/views 正在尝试正确加入。他们是 V_ARINVOICE_ALLV_GLBATCH_AR_COGS。下图中的第三个table V_ARINVOICE_ALL只是为了额外说明。

Click Here

我目前得到错误的返回行数,我认为是因为某种交叉连接。我正在尝试从 V_GLBATCH_AR_COGS 加入 Credit

我使用以下 SQL 查询来加入 V_ARINVOICE_ALLV_ARINVOICE_ALL 并且我得到了所有正确的结果,除了未选择的`Credit:

SELECT 
V_ARINVOICE_ALL.INVOICE_DATE,
V_ARINVOICE_ALL.ARCUSTO_COMPANY,
V_ARINVOICE_ALL.ARCUSTO_CUSTNO,
V_ARINVOICE_ALL.INVOICE_NO


FROM   
IQMS.V_ARINVOICE_ALL V_ARINVOICE_ALL 


INNER JOIN
IQMS.V_GLACCT V_GLACCT
ON
  V_ARINVOICE_ALL.GLACCT_ID_SALES = V_GLACCT.ID AND V_GLACCT.ACCT = '3648-00-0'



WHERE 
V_ARINVOICE_ALL.ARCUSTO_CUSTNO = 'OX01' 
AND 
V_ARINVOICE_ALL.INVOICE_DATE >= TO_DATE('05/04/2016', 'dd/mm/yyyy')
AND
V_ARINVOICE_ALL.INVOICE_DATE <= TO_DATE('15/06/2017', 'dd/mm/yyyy')

ORDER BY 
 V_ARINVOICE_ALL.INVOICE_DATE 

Result (41 Rows)

  |INVOICE_DATE|...| INVOICE|
  +------------+---+--------+
  |  05/04/2016|...|  494658|
  |  20/05/2016|...|  495274|
  |  20/05/2016|...|  495275|

但是当我尝试使用以下查询加入 V_ARINVOICE_ALLV_GLBATCH_AR_COGS 时:

SELECT 
 V_ARINVOICE_ALL.INVOICE_DATE,
 V_ARINVOICE_ALL.ARCUSTO_COMPANY,
 V_ARINVOICE_ALL.ARCUSTO_CUSTNO,
 V_ARINVOICE_ALL.INVOICE_NO,
 V_GLBATCH_AR_COGS.CREDIT AS "INCOME"


FROM   
 IQMS.V_ARINVOICE_ALL V_ARINVOICE_ALL 


INNER JOIN
 IQMS.V_GLBATCH_AR_COGS V_GLBATCH_AR_COGS
ON
 V_ARINVOICE_ALL.GLBATCHID_ID = V_GLBATCH_AR_COGS.ID
 AND V_ARINVOICE_ALL.GLPERIODS_ID_AR = V_GLBATCH_AR_COGS.GLPERIODS_ID
 AND V_GLBATCH_AR_COGS.DESCRIP LIKE '%OX01%ITEMS%' AND V_GLBATCH_AR_COGS.ACCT = '3648-00-0'



WHERE 
 V_ARINVOICE_ALL.ARCUSTO_CUSTNO = 'OX01' 
AND 
 V_ARINVOICE_ALL.INVOICE_DATE >= TO_DATE('05/04/2016', 'dd/mm/yyyy')
AND
 V_ARINVOICE_ALL.INVOICE_DATE <= TO_DATE('15/06/2017', 'dd/mm/yyyy')

ORDER BY 
 V_ARINVOICE_ALL.INVOICE_DATE 

I get something like: (165 Rows)

  |INVOICE_DATE|...| INVOICE|  INCOME|
  +------------+---+--------+--------+
  |  ...       |...|  ...   |   ...  |
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     450|
  |  23/06/2016|...|  495667|     450|
  |  23/06/2016|...|  495667|     450|
  |  ...       |...|  ...   |   ...  |

Desired result (41 Rows)

  |INVOICE_DATE|...| INVOICE|  INCOME|
  +------------+---+--------+--------+
  |  ...       |...|  ...   |   ...  |
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     450|
  |  ...       |...|  ...   |   ...  |

我比以前更熟悉联接了。但我不确定我做对了。我试过右连接、右外连接、左连接、左外连接等。我在这里做错了什么?


EDIT2:第一次编辑很糟糕,毫无意义。我不知道这是否有帮助,但只有两行

  |  23/06/2016|...|  495667|     225|
  |  23/06/2016|...|  495667|     225|

在 table 中匹配 Join 上的 WHERE 子句:V_ARINVOICE_ALL.GLPERIODS_ID_AR = V_GLBATCH_AR_COGS.GLPERIODS_ID AND V_GLBATCH_AR_COGS.DESCRIP LIKE '%OX01%ITEMS%' AND V_GLBATCH_AR_COGS.ACCT = '3648-00-0

但还是会得到两次以上。所以我想这与一对多的关系有关。我不确定如何处理。

如果有帮助的话。


如果您需要更多信息,请随时询问。

看起来像是一对多的关系。 COGS table 中的值比您需要的多(可能是另一列填充了其他数据)。

尝试SELECT DISTINCT限制所选列的唯一值:

SELECT DISTINCT -- DISTINCT goes here
 V_ARINVOICE_ALL.INVOICE_DATE,
 V_ARINVOICE_ALL.ARCUSTO_COMPANY,
 V_ARINVOICE_ALL.ARCUSTO_CUSTNO,
 V_ARINVOICE_ALL.INVOICE_NO,
 V_GLBATCH_AR_COGS.CREDIT AS "INCOME"


FROM   
 IQMS.V_ARINVOICE_ALL V_ARINVOICE_ALL 


INNER JOIN
 IQMS.V_GLBATCH_AR_COGS V_GLBATCH_AR_COGS
ON
 V_ARINVOICE_ALL.GLBATCHID_ID = V_GLBATCH_AR_COGS.ID
 AND V_ARINVOICE_ALL.GLPERIODS_ID_AR = V_GLBATCH_AR_COGS.GLPERIODS_ID
 AND V_GLBATCH_AR_COGS.DESCRIP LIKE '%OX01%ITEMS%' AND V_GLBATCH_AR_COGS.ACCT = '3648-00-0'



WHERE 
 V_ARINVOICE_ALL.ARCUSTO_CUSTNO = 'OX01' 
AND 
 V_ARINVOICE_ALL.INVOICE_DATE >= TO_DATE('05/04/2016', 'dd/mm/yyyy')
AND
 V_ARINVOICE_ALL.INVOICE_DATE <= TO_DATE('15/06/2017', 'dd/mm/yyyy')

ORDER BY 
 V_ARINVOICE_ALL.INVOICE_DATE