Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有如下表,我想创建一个每天唯一活跃用户的视图。每个用户都有一个开始和结束日期,但有些日子是重叠的。我该如何解决这个问题?
您可以取消透视数据并使用聚合和窗口函数。这看起来像:
select dte, sum(inc) as change_on_day, sum(sum(inc)) over (order by dte) as active_on_day from ((select user, start_date as dte, 1 as inc from t) union all (select user, end_date, -1 from t) ) t group by dte order by dte;