有人能帮我吗?
我有两个表,表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)
1条答案
按热度按时间daupos2t1#
请参阅SQL 0338 N的说明:
SQL 0338 N与JOIN运算符关联的ON子句或MERGE语句中的ON子句无效。
说明
与JOIN运算符关联的ON子句或MERGE语句中的ON子句无效,原因如下。
*MERGE语句的ON子句不能包含子查询。
无法处理该语句。
如果
(Product_id, Order_no)
是结果集的“唯一键”,则“基本查询”(不需要分页)位于适合分页的完整查询中。其结果是:
| 产品标识|订单编号|更新_TS|订单状态|
| - -|- -|- -|- -|
| 小行星1234569|聚乙二醇单甲醚|2021-08-05-06.01.02.000145 |P值|
| 小行星1234575| M12347型|||