【发布时间】:2019-07-09 10:03:28
【问题描述】:
我想查找日期范围 1/1/19-1/7/19 中增加金额的记录
使用表HISTORY:
日期金额 ID
(日期,数字,varchar2(30))
我正确地找到了范围内的 ID
假设只有当有两个具有相同 ID 的记录时才会发生增加/减少
with suspect as
(select id
from history
where t.createddate < to_date('2019-07-01', 'yyyy-mm-dd')
group by id
having count(1) > 1),
ids as
(select id
from history
join suspect
on history.id = suspect.id
where history.date > to_date('2019-01-01', 'yyyy-mm-dd')
and history.date < to_date('2019-07-01', 'yyyy-mm-dd'))
select count(distinct id)
from history a, history b
where a.id = b.id
and a.date < b.date
and a.amount < b.amount
寻找增加的问题我需要找到以前的记录,它可以在时间范围之前
我可以找到时间范围之前的最后一次,但我没有使用它:
ids_prevtime as (
select history.*, max(t.date) over (partition by t.id) max_date
from history
join ids on history.userid = ids.id
where history.date < to_date('2019-01-01','yyyy-mm-dd' )
), ids_prev as (
select * from ids_prevtime where createdate=max_date
)
【问题讨论】:
标签: sql oracle date-range