【发布时间】:2021-04-18 13:27:44
【问题描述】:
为什么这两个查询有两个不同的结果:
- 第一个查询:
select c.customer_id, oo.order_id from co.CUSTOMERS c outer apply (select * from co.orders o where o.customer_id = c.CUSTOMER_ID order by o.order_id desc fetch first 1 rows only) oo where c.CUSTOMER_ID = 22 and oo.order_id = 10000
- 第二次查询:
select c.customer_id, oo.order_id from co.CUSTOMERS c outer apply (select * from co.orders o where o.customer_id = c.CUSTOMER_ID order by o.order_id desc fetch first 1 rows only) oo where c.CUSTOMER_ID = 22 and oo.order_id = 10000 group by c.customer_id, oo.order_id
co.CUSTOMERS c 的数据
select * from co.CUSTOMERS c where c.customer_id=22
| CUSTOMER_ID | EMAIL_ADDRESS | FULL_NAME |
|---|---|---|
| 22 | raymond.bailey@internalmail | Raymond Bailey |
co.orders的数据
select * from co.orders o where o.customer_id=22
order by o.order_id desc
| ORDER_ID | ORDER_DATETIME | CUSTOMER_ID | ORDER_STATUS | STORE_ID |
|---|---|---|---|---|
| 1819 | 11-MAR-19 07.20.17.942291 AM | 22 | COMPLETE | 22 |
| 1435 | 07-JAN-19 03.57.14.316173 PM | 22 | COMPLETE | 1 |
| 511 | 19-JUL-18 06.24.36.024192 AM | 22 | COMPLETE | 1 |
| 184 | 26-APR-18 05.13.15.426435 AM | 22 | COMPLETE | 1 |
第一个查询不返回任何内容
但是第二个查询返回如下:
| CUSTOMER_ID | ORDER_ID |
|---|---|
| 22 | 10000 |
【问题讨论】:
-
你能发布一个可重现的测试用例吗?最好是我们可以复制和粘贴的文本或指向 dbfiddle.uk/liveSQL/ 等的链接。您已经发布了两个表之一的数据,但没有发布另一个。
-
你的 oracle 版本是什么?和补丁级别?
-
我在 oracle 12.c 和 19 (livesql.oracle.com) 上测试
标签: sql oracle group-by outer-apply