0

我正在尝试编写一些用于选择报告数据的 SQL。数据来自 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 - 工单主

  • WASRST(WO 状态码)是一个长度为 2 的字符串。它是一组枚举值。

F0911 - 帐户分类帐

  • GLSBL(总帐子分类帐)可能包含来自工作订单路由 (F3112.WLDOCO) 的凭证编号。需要注意的重要一点是 GLSBL 是一个字符串,长度为 8 并且值用零填充。F3112.WLDOCO 是长度为 8 的数值(无填充)。这就是为什么我在 JOIN 子句中有 CAST 的原因。
  • GLOBJ(Object account)是一个长度为6的字符串,不知道有没有padding。

此查询将不会运行。它给了我一个有点神秘的“数据转换或映射错误”。我无法弄清楚有问题的 SQL 是什么。

现在这是奇怪的部分。如果我稍微修改此查询以仅使用F4801F0911加入 F3112 ,则任一形式的查询都有效。我只是不能让它们作为一个查询一起工作(这里有线索吗?)。

所以,为了说明:

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'

这些都工作得很好。

4

3 回答 3

0

试试这样:

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'
于 2021-03-19T04:00:08.023 回答
0

谢谢@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。

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

于 2021-03-19T15:43:32.593 回答
0

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

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
于 2021-03-18T20:29:22.553 回答