为什么这个 SQL 不起作用? [JD 爱德华兹 DB2]

Why doesn't this SQL work? [JD Edwards DB2]

我正在尝试编写一些 SQL 用于报告的 select 数据。数据来自 JD Edwards,使用 AS/400.

上的 DB2 数据库

我写了以下 SQL 查询:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL  AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F0911.GLOBJ = '6855'
       AND F4801.WASRST BETWEEN '30' AND '95'

其中:

F3112 - 工单路由

F4801 - 工单主管

F0911 - 账户分类帐

这个查询不会运行。它给了我一个有点神秘的“数据转换或映射错误”。我还没弄明白有什么问题 SQL。

现在是奇怪的部分。如果我稍微修改此查询以将 F3112 与 ONLY F4801 OR F0911 联接,则两种查询形式均有效。我只是无法让它们作为一个查询一起工作(这里有线索吗?)。

所以,为了说明:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
WHERE  F0911.GLOBJ = '6855'

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F4801.WASRST BETWEEN '30' AND '95'

这两个都很好用。

请执行此操作,如果遇到任何错误请告诉我。

Select * from 
      (SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
WHERE  F0911.GLOBJ = '6855')a
inner join 

(SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F4801.WASRST BETWEEN '30' AND '95')b
on a.WLDOCO=b.WLDOCO

这样试试:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL  AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
         AND F0911.GLOBJ = '6855'
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
       AND F4801.WASRST BETWEEN '30' AND '95'

感谢@nfgl。使用 DIGITS 解决了“数据转换”问题。

我尝试了上面的两个答案建议(它们在我使用 DIGITS 而不是 CAST 后起作用)并且经过一些实验和调整,我基本上在所有情况下都得到了相同的结果。

我的 SQL 现在看起来像这样:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL  AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON DIGITS(F3112.WLDOCO) = F0911.GLSBL
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F0911.GLOBJ = '6855'
       AND F4801.WASRST BETWEEN '30' AND '95'

与我原来的唯一区别是在第一个内部连接的 ON 条件中使用了 DIGITS。

认为这让我得到了我需要的东西。如果有人看到问题,请告诉我。