sql - 如果有活动日期,则以 0 重新开始行号
问题描述
如何使用 Active 作为最后登录日期填充上个月登录列?
解决方案
您可以使用累积最大值来获取最近的active
日期。诀窍就是几个月的差异。要获取最近的活动日期:
select t.*,
max(active) over (partition by userid order by date) as most_recent_active
from t;
然后是之间的几个月:
select t.*,
(extract(year from most_recent_active - date) * 12 +
extract(month from most_recent_active)
) as months_since
from (select t.*,
max(active) over (partition by userid order by date) as most_recent_active
from t
) t;
推荐阅读
- c++ - 当代码从高级语言转换为低级语言时,访问说明符是否仍然完好无损?
- cmd - 如何在 Atom-IDE 嵌入式终端中以管理员身份打开 cmd
- reactjs - 如何将道具传递给我的孩子路线?
- laravel - 使用依赖注入和自定义请求测试 Laravel API 资源
- java - 我可以在java中看到实时更改的时区吗?
- angular - FIREBASE 警告:无法读取未定义的属性“myID”
- facebook-graph-api - Facebook API v3 - publish_to_groups 不工作
- visual-studio - Visual Studio 2017 - 创建带有解决方案和位置的 C# 控制台应用程序 (.NET Framework)
- javascript - Chrome 扩展程序权限在加载时出错
- java - 对 BCryptPasswordEncoder 和加盐感到困惑