我有以下几行:
DateTime Item_no Vo_No
2019-06-27 30322264 0118113
2017-12-27 00265929 0242712
2019-01-21 30322264 0515768
2017-12-27 00265929 0400026
2019-01-21 30322264 0569606
2018-09-25 00265929 0696864
2019-01-21 30317757 0696619
2019-06-27 30317757 0118113
2017-12-27 00265929 0242624
2017-01-24 00265929 0282971
2019-01-21 30317757 0386202
2019-01-21 30317757 0515706
2019-01-21 30322264 0696619
2017-12-27 00265929 0242625
2017-12-27 00265929 0395347
2017-12-27 00265929 0441449
2019-01-21 30317757 0569605
2017-12-27 00265929 0282972
2017-01-24 00265929 0282984
2019-01-21 30322264 0397256
我正在尝试获取具有最新日期和vo_no的独特商品编号。
我提出了以下查询:
SELECT MAX(b.upd_dtime), b.item_no
FROM vo_item_t b JOIN (
SELECT a.vo_no, a.vo_state, a.vo_confirm_date, a.vo_order_date
FROM vo_order_t a) y ON y.vo_no = b.vo_no and b.item_No IN(30317757, 30322264, 00265929)
GROUP BY b.item_no
效果很好,我得到以下结果:
Date Item_No
2019-06-27 30322264
2019-06-27 30317757
2018-09-25 00265929
但是您可以看到,每个Item_No都缺少vo_no,所以我尝试了以下操作:
SELECT MAX(b.upd_dtime), b.item_no, y.vo_no
FROM vo_item_t b JOIN (
SELECT a.vo_no, a.vo_state, a.vo_confirm_date, a.vo_order_date
FROM vo_order_t a) y ON y.vo_no = b.vo_no and b.item_No IN(30317757, 30322264, 00265929)
GROUP BY b.item_no, y.vo_no
如您所见,我在select和group by中添加了y.vo_no。但是查询的结果如下:
2019-06-27 30322264 0118113
2017-12-27 00265929 0242712
2019-01-21 30322264 0515768
2017-12-27 00265929 0400026
2019-01-21 30322264 0569606
2018-09-25 00265929 0696864
2019-01-21 30317757 0696619
2019-06-27 30317757 0118113
2017-12-27 00265929 0242624
2017-01-24 00265929 0282971
2019-01-21 30317757 0386202
2019-01-21 30317757 0515706
2019-01-21 30322264 0696619
2017-12-27 00265929 0242625
2017-12-27 00265929 0395347
2017-12-27 00265929 0441449
2019-01-21 30317757 0569605
2017-12-27 00265929 0282972
2017-01-24 00265929 0282984
2019-01-21 30322264 0397256
item_no上的区别项不再起作用。
所以我的问题是:我又如何获得不同item_no的vo_no?
答案 0 :(得分:2)
您可以使用内部联接来解决此问题。可能有一种更简单的方法,但目前应该可以使用
select x.*,y.vo_no from (
SELECT MAX(b.upd_dtime) as MaxT, b.item_no as Item_NO
FROM vo_item_t b JOIN (
SELECT a.vo_no, a.vo_state, a.vo_confirm_date, a.vo_order_date
FROM vo_order_t a) y ON y.vo_no = b.vo_no and b.item_No IN(30317757, 30322264, 00265929)
GROUP BY b.item_no
)x
INNER JOIN
vo_order_t y
ON x.MaxT = y.Date
AND x.Item_NO = y.Item_No
答案 1 :(得分:1)
还有此解决方案:
select item_no, max(datetime) mxdt, max(vo_no) keep(dense_rank first order by datetime desc) vo_no
from (select '2019-06-27' datetime, 30322264 item_no, 0118113 vo_no from dual union
select '2017-12-27', 00265929 , 0242712 from dual union
select '2019-01-21', 30322264 , 0515768 from dual union
select '2017-12-27', 00265929 , 0400026 from dual union
select '2019-01-21', 30322264 , 0569606 from dual union
select '2018-09-25', 00265929 , 0696864 from dual union
select '2019-01-21', 30317757 , 0696619 from dual union
select '2019-06-27', 30317757 , 0118113 from dual union
select '2017-12-27', 00265929 , 0242624 from dual union
select '2017-01-24', 00265929 , 0282971 from dual union
select '2019-01-21', 30317757 , 0386202 from dual union
select '2019-01-21', 30317757 , 0515706 from dual union
select '2019-01-21', 30322264 , 0696619 from dual union
select '2017-12-27', 00265929 , 0242625 from dual union
select '2017-12-27', 00265929 , 0395347 from dual union
select '2017-12-27', 00265929 , 0441449 from dual union
select '2019-01-21', 30317757 , 0569605 from dual union
select '2017-12-27', 00265929 , 0282972 from dual union
select '2017-01-24', 00265929 , 0282984 from dual union
select '2019-01-21', 30322264 , 0397256 from dual)
group by item_no
order by item_no
将其显示在您的数据上
ITEM_NO MXDT VO_NO
---------- ---------- ----------
265929 2018-09-25 696864
30317757 2019-06-27 118113
30322264 2019-06-27 118113
(查找this doc page)