0

我正在尝试在我的基本查询中创建一个子查询(针对特定列)。代码如下。

 SELECT    z.po_id,
          max
              (
              select   etcdc.ship_evnt_tms
              FROM     covinfos.shipment_event etcdc
              WHERE    etcdc.ship_evnt_cd = '9P'
              AND      etcdc.ship_id=scdc.ship_id
              ORDER BY etcdc.updt_job_tms desc
              FETCH first ROW only) AS llp_estimated_delivery_cdc
FROM      covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON        z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON        (
                    a.ship_id = scdc.ship_id
          AND       a.ship_to_loc_code = scdc.ship_to_loc_code
          AND       scdc.loc_type = 'CDC')
GROUP BY  z.po_id 

根据我尝试运行代码时弹出的错误消息,某处似乎存在某种错字。

BIC00004. DAL01008. An error occurred while accessing the database.
ILLEGAL SYMBOL ".". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: , ). SQLCODE=-104, 
SQLSTATE=42601, DRIVER=3.62.56; THE CURSOR SQL_CURLH200C1 IS NOT IN A 
PREPARED STATE. SQLCODE=-514, SQLSTATE=26501, DRIVER=3.62.56

然而,在显而易见的地方,或者至少在我看来,没有什么可以发现错误。此外,在一张空白表中运行子选择(在基本查询之外,新的)它可以正确运行。

谢谢

4

2 回答 2

1

您可能最好删除共同相关的子选择,然后加入一个普通的子选择。例如

SELECT    z.po_id,
          max(ship_evnt_tms) AS llp_estimated_delivery_cdc
FROM      covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON        z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON        a.ship_id = scdc.ship_id
AND       a.ship_to_loc_code = scdc.ship_to_loc_code
AND       scdc.loc_type = 'CDC'
LEFT JOIN
    (    select ship_id
         ,      ship_evnt_tms
         FROM
         (   select  ship_id
            ,        ship_evnt_tms
            ,        row_number() over(partition by ship_id order by updt_job_tms desc) as RN
            FROM     covinfos.shipment_event
            WHERE    ship_evnt_cd = '9P'
         ) s
         WHERE RN = 1
    )  AS etcdc
ON        etcdc.ship_id=scdc.ship_id
GROUP BY  z.po_id 

PS 你可以只使用 INNER JOIN,除非你想包含 po_id 而没有 ship_evnt_tms

于 2018-04-18T18:15:57.977 回答
0

尝试在子选择周围添加括号。至少这会使用 z/OS 验证解析 Data Studio

SELECT    z.po_id,
          max
              ((
              select   etcdc.ship_evnt_tms
              FROM     covinfos.shipment_event etcdc
              WHERE    etcdc.ship_evnt_cd = '9P'
              AND      etcdc.ship_id=scdc.ship_id
              ORDER BY etcdc.updt_job_tms desc
              FETCH first ROW only)) AS llp_estimated_delivery_cdc
FROM      covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON        z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON        (
                    a.ship_id = scdc.ship_id
          AND       a.ship_to_loc_code = scdc.ship_to_loc_code
          AND       scdc.loc_type = 'CDC')
GROUP BY  z.po_id 

不过,我不确定这是一段非常好的 SQL 代码。

于 2018-04-18T15:45:58.467 回答