我有两个要正确加入的表/视图。它们是V_ARINVOICE_ALL
和V_GLBATCH_AR_COGS
。下图中的第三个表格V_ARINVOICE_ALL
仅用于额外说明。
我目前正在返回错误的行数,因为我认为是一种交叉连接。我正在尝试Credit
从V_GLBATCH_AR_COGS
.
我使用以下 SQL 查询来加入V_ARINVOICE_ALL
,V_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_ALL
并V_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
但仍然会得到它超过两次。所以我想这与一对多的关系有关。我不知道如何处理。
如果这有任何帮助。
如果您需要更多信息,请随时询问。