WITH A AS (
SELECT SYSDATE date_1,'1' data_1 ,1 item FROM DUAL
UNION
SELECT add_months(SYSDATE,7) date_1,'8'data, '1' item_ FROM DUAL
UNION
SELECT add_months(SYSDATE,6)+ 10, '4' data, '1' item FROM dual
UNION
SELECT add_months(SYSDATE,6) date_1,'3' data, '2' item FROM DUAL
)
SELECT min(date_1), data,item
FROM a
WHERE date_1 BETWEEN add_months(SYSDATE,6) AND last_day(add_months(SYSDATE,7))
GROUP BY item,data
结果显示项目 1 有两行,但我只想要最小日期一,如何更改代码以在最小(日期)显示一项数据。*我需要日期匹配正确的数据和项目 *。感谢您阅读并期待正确的答案。
我找到了一种不好的方法来调用我的意思:按项目分组并在范围内选择最小 date_1
WITH A AS (
SELECT SYSDATE date_1, '1' data, '1' item FROM DUAL
UNION SELECT add_months(SYSDATE,7) date_1, '8' data, '1' item FROM DUAL
UNION SELECT add_months(SYSDATE,6)+ 10, '4' data, '1' item FROM dual
UNION SELECT add_months(SYSDATE,6) date_1, '3' data, '2' item FROM DUAL
)
select a.* from A a,(select min(date_1) date_1,item from A
where
date_1 BETWEEN add_months(SYSDATE, 6)
AND last_day(add_months(SYSDATE, 7))
group by item) b where a.date_1 = b.date_1 and a.item= b.item