查找具有其他属性的最小最大值?

Find min max with other attribute?

假设我有 table time_created, view_type, view_id Clickhouse 中的列。

如何找到按 view_type 适当分组的 time_created 的最小值和最大值 time_created

这是开始 sql:

select min(time_created) as min_time_created,
max(time_created) as max_time_created,
view_type  from views 
group by view_type;

以及如何将 view_id 添加到每个结果列。还考虑到 time_created 不是唯一的。

看起来这个查询应该有帮助:

SELECT view_type, 
  min(time_created) as min_time_created,
  max(time_created) as max_time_created
  argMin(view_id, time_created) as min_view_id, 
  argMax(view_id, time_created) as max_view_id
FROM views
GROUP BY view_type

试试这个并使用 window 函数

Select 
      MIN(time_created) OVER (PARTITION BY view_type) AS Min_time_created,
      MAX(time_created) OVER (PARTITION BY view_type) AS Min_time_created,
      View_type  
from Views ;