0

我有两个要正确加入的表/视图。它们是V_ARINVOICE_ALLV_GLBATCH_AR_COGS。下图中的第三个表格V_ARINVOICE_ALL仅用于额外说明。

点击这里

我目前正在返回错误的行数,因为我认为是一种交叉连接。我正在尝试CreditV_GLBATCH_AR_COGS.

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

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 

结果(41 行)

  |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 

我得到类似的东西:(165行)

  |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|
  |  ...       |...|  ...   |   ...  |

期望的结果(41 行)

  |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|

WHERE在与 Join 上的子句匹配的表中: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

但仍然会得到它超过两次。所以我想这与一对多的关系有关。我不知道如何处理。

如果这有任何帮助。


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

4

1 回答 1

0

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

尝试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 
于 2017-08-08T09:38:21.247 回答