[英]How to get date increments in between start date and end date in MYSQL
[英]How to get the input date between start and end date in oracle if input date not available then return max date record
我已经获取了相同的数据并从中创建了一个表格demo
。 见dbfiddle
您可以尝试以下查询,
WITH data AS
(SELECT DATE '2020-08-14' input FROM dual) --input parameter
SELECT arng_id,st_dt,end_dt
FROM (SELECT input
,demo.*
,MAX(end_dt) over(ORDER BY NULL) max_dt --taken max(end_dt) to match in case no result with input parameter
FROM demo
,data)
WHERE (input BETWEEN st_dt AND end_dt) -- matches with input paramater OR
OR (max_dt BETWEEN end_dt AND input) -- condition when no result with first match
ORDER BY end_dt DESC -- order by require to fetch the max record always
FETCH FIRST 1 ROW ONLY --oracle 12c feature
对于 11g,我们可以使用ROWNUM
。 见dbfiddle
注意:我没有考虑提供的输入小于 max(end_dt) 并且与任何行都不匹配的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.