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

tvmytwxo  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(91)

有人能帮我吗?
我有两个表,表A有产品ID和订单号作为键,表B有产品ID、订单号(作为键)沿着Update_Timestamp、订单状态等;我尝试获取表A中存在的所有匹配的Product-Id和Order-number,这些匹配项具有a)表B中的匹配记录-具有表B中订单的最新时间戳b)表B中的非匹配记录,以便在结果集中同时获取a)和b),如下所示。

产品(表A)

| 产品标识(_I)|订单编号(_N)|
| - -|- -|
| 小行星1234567|编号S12345|
| 小行星1234568| S12346型|
| 小行星1234569|聚乙二醇单甲醚|
| 小行星1234575| M12347型|

订单详情(表B)

| 产品标识(_I)|订单编号(_N)|更新(_T)|订单状态|
| - -|- -|- -|- -|
| 小行星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|聚乙二醇单甲醚|2021-05-05-06.01.02.000145 |C类|
| 小行星1234569|聚乙二醇单甲醚|2021-06-05-06.01.02.000145 |C类|
| 小行星1234569|聚乙二醇单甲醚|2021-07-05-06.01.02.000145|米|
| 小行星1234569|聚乙二醇单甲醚|2021-08-05-06.01.02.000145 |P值|
| 小行星1234574| T12347型|2021-07-05-06.01.02.000145 |P值|

预期结果

| 产品标识(_I)|订单编号(_N)|更新(_T)|订单状态|
| - -|- -|- -|- -|
| 小行星1234569|聚乙二醇单甲醚|2021-08-05-06.01.02.000145 |P值|
| 小行星1234575| M12347型|空值|空值|
查询:如果需要筛选Order_stat,比如说产品标识为“P”的订单状态,如果所有匹配订单沿着不匹配记录的订单状态都具有最新时间戳,则两个表中都存在具有最新时间戳的订单编号,如上所述?
编辑:尝试以下查询,但出现错误: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)
daupos2t

daupos2t1#

请参阅SQL 0338 N的说明:

SQL 0338 N与JOIN运算符关联的ON子句或MERGE语句中的ON子句无效。
说明

与JOIN运算符关联的ON子句或MERGE语句中的ON子句无效,原因如下。

  • ON子句中的列引用只能引用ON子句范围内的表的列。
  • 在完全外部联接的ON子句中引用的函数必须是确定性的,并且没有外部操作。
  • 不能使用取消引用操作(-〉)。
    *MERGE语句的ON子句不能包含子查询。
  • MERGE语句的ON子句不能包含标量fullselect。
  • MERGE语句的ON子句不能包含内联SQL函数或内联SQL方法。

无法处理该语句。
如果(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

其结果是:
| 产品标识|订单编号|更新_TS|订单状态|
| - -|- -|- -|- -|
| 小行星1234569|聚乙二醇单甲醚|2021-08-05-06.01.02.000145 |P值|
| 小行星1234575| M12347型|||

相关问题