DB2 NSP SQL 错误,左连接的最大时间戳

DB2 NSP SQL error, max time stamp on left join

谁能帮我解决这个问题?

我有两个 tables,Table A 以产品 ID 和订单号为键,Table B 以产品 ID、订单号 (作为键)以及 Update_Timestamp、订单状态等。 ;正在尝试获取 Table A 中存在的所有匹配的 Product-Id 和 Order-number 已 a) table B 中的匹配记录 - 具有来自 table B 的订单的最新时间戳 b) Table B 中的不匹配记录 在结果集中同时获得 a) 和 b),如下所示。

产品 (Table A)

Product_id Order_no
1234567 S12345
1234568 S12346
1234569 P12347
1234575 M12347

订单详情(Table B)

Product_id Order_no Update_ts Order_stat
1234567 S12345 2020-05-05-05.01.02.123455 P
1234567 S12345 2020-06-05-05.01.02.123455 P
1234567 S12345 2020-07-05-05.01.02.123455 C
1234568 S12346 2021-05-05-06.01.02.123456 C
1234569 P12347 2021-05-05-06.01.02.000145 C
1234569 P12347 2021-06-05-06.01.02.000145 C
1234569 P12347 2021-07-05-06.01.02.000145 M
1234569 P12347 2021-08-05-06.01.02.000145 P
1234574 T12347 2021-07-05-06.01.02.000145 P

预期结果

Product_id Order_no Update_ts Order_stat
1234569 P12347 2021-08-05-06.01.02.000145 P
1234575 M12347 NULL NULL

查询:如果 Order_stat 需要过滤,比如产品 ID 的 'P' 订单状态,如果该订单状态存在,则订单号存在于两个 table 中并带有最新戳记所有匹配订单的最新时间戳以及上面不匹配的记录? .

编辑:尝试了以下查询,但出现错误:AN‬‎ ‪ON‬‎ ‪CLAUSE‬‎ ‪IS‬‎ ‪INVALID‬‎.‪‬‎ ‪SQLCODE‬‎=‪‬‎-‪338‬‎,‪‬‎ ‪SQLSTATE‬‎=‪42972‬‎,‪‬‎ ‪DRIVER‬‎=‪4‬‎.‪24‬‎.‪92。此错误已通过建议的最新查询得到解决,并希望对上述查询提出任何建议 您对如何获得预期结果有什么建议吗?

 select 
   aa.Product_id 
  ,aa.Order_no
  ,bb.Update_ts
  ,bb.Order_stat
  from
  Product         aa
  Left join
  Orderdetails    bb
  on   aa.product_id       =bb.product_id
  and  aa.order_no         =bb.order_no
  and  bb.update_ts        =(select max(cc.update_ts) from Orderdetails cc  
                                  where cc.product_id  = bb.product_id
                                    and cc.order_no    = bb.order_no
                                    and cc.order_stat  = bb.order_stat) 

参考SQL0338N的解释:

SQL0338N An ON clause associated with a JOIN operator or in a MERGE statement is not valid.

Explanation
An ON clause associated with a JOIN operator or in a MERGE statement is not valid for one of the following reasons.

  • Column references in an ON clause must only reference columns of tables that are in the scope of the ON clause.
  • A function referenced in an ON clause of a full outer join must be deterministic and have no external action.
  • A dereference operation (->) cannot be used.
  • The ON clause of a MERGE statement cannot include a subquery.
  • The ON clause of a MERGE statement cannot include a scalar fullselect.
  • The ON clause of a MERGE statement cannot include an inlined SQL function or an inlined SQL method.

The statement cannot be processed.

改为使用以下内容。如果 (Product_id, Order_no) 是结果集的“唯一键”,则“基本查询”(无需分页)位于适合分页的完整查询内。

select Product_id, Order_no, Update_ts, Order_stat
from
(
select 
  a.*
-- Enumeration for pagination  
, row_number () over (order by Product_id, Order_no) as rn_
from
(
-- Base query start
select 
  aa.Product_id 
, aa.Order_no
, bb.Update_ts
, bb.Order_stat
from Product aa
join 
(
  select 
    o.*
  , row_number () over (partition by product_id, order_no order by update_ts desc) as rn_ 
  from Orderdetails o
) bb on bb.product_id = aa.product_id and bb.order_no = aa.order_no 
and bb.order_stat = 'P' and bb.rn_ = 1

  union all

select 
  aa.Product_id 
, aa.Order_no
, NULL AS Update_ts
, NULL AS Order_stat
from Product aa
where not exists 
(
select 1 
from Orderdetails bb 
where bb.product_id = aa.product_id and bb.order_no = aa.order_no
)
-- Base query end
) a
) b
-- Use of the enumeration for pagination
where rn_ between 1 and 2
order by 1, 2

结果是:

PRODUCT_ID ORDER_NO UPDATE_TS ORDER_STAT
1234569 P12347 2021-08-05-06.01.02.000145 P
1234575 M12347