从审核表中为每个用户ID选择旧价格

时间:2020-08-12 10:15:13

标签: sql

此联接可用于我要执行的操作,但速度很慢,而且似乎效率很低。

基本上,最里面的查询告诉我appeal_audit.price何时更改,最外面的查询仅向我显示其更改的值,最外面的查询仅向我显示最早的一次停止重复复制的值

left join(
select * from (
Select
ROW_NUMBER() over (partition by UserId order by auditdate) as RowNo,
userid,
OldPrice,
auditdate
from (
select
UserId,
Price,
lag(appeal_audit.price) over (partition by appeal_audit.userid order by appeal_audit.auditdate) as OldPrice,
case when appeal_audit.Price <> lag(appeal_audit.price) over (partition by appeal_Audit.userid order by appeal_audit.auditdate) then 1 else 0 end as Flag,
AuditDate
from Appeal_Audit) t4
where t4.flag = 1) t5
where t5.RowNo = 1 ) t6
on t6.userid = userdetails.userid

0 个答案:

没有答案